...

View Full Version : detect if db table exists by php



gilgalbiblewhee
03-03-2009, 06:42 PM
I know how to create a table:
http://www.w3schools.com/php/php_mysql_create.asp
But how can I detect if the table exists. And if it doesn't, then create one using php?

Fumigator
03-03-2009, 09:03 PM
If you try to select a row from the table you'll receive error "1146" back if the table doesn't exist. You can use the mysql_errno() function to check for this error.

Full list of error numbers:

http://dev.mysql.com/doc/refman/5.1/en/error-messages-server.html

CFMaBiSmAd
03-03-2009, 09:06 PM
Or depending on the context of when you want to do this, just use the IF NOT EXISTS form of the CREATE TABLE syntax -


CREATE TABLE IF NOT EXISTS tbl_name
(create_definition,...)
[table_option] ...

Fumigator
03-03-2009, 09:51 PM
Oh yes, that is a better solution. I was thinking there was an option like that for CREATE TABLE, but I got distracted and forgot to look it up.

gilgalbiblewhee
03-04-2009, 02:10 AM
Or depending on the context of when you want to do this, just use the IF NOT EXISTS form of the CREATE TABLE syntax -
this is the url:
book1=1&chapter1=46&verse1=all&book2=2&chapter2=1&verse2=all&txtarea0=these are the names of the children of Israel, which came into Egypt
For some reason after book1 everything else shows blank.


<?php
$db = "kjv";
$table = "findings";
include("dbconnection.php");
// Create table
mysql_select_db($db, $con);
$sql = "CREATE TABLE IF NOT EXISTS findings
(
ID MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
book1 int,
chapter1 int,
verse1 VARCHAR(4),
book2 int,
chapter2 int,
verse2 VARCHAR(4),
txtarea0 VARCHAR(60),
txtarea1 VARCHAR(60),
txtarea2 VARCHAR(60),
txtarea3 VARCHAR(60),
txtarea4 VARCHAR(60),
txtarea5 VARCHAR(60),
comments VARCHAR(60)
)";

// Execute query
mysql_query($sql, $con);
//put your table creation code here
//}
/************************************************************************************************/
$con = mysql_connect("","root","");
if (!$con)
{
die("Could not connect: ".mysql_error());
}
mysql_select_db($db, $con);
/*
book1=1&chapter1=46&verse1=all&book2=2&chapter2=1&verse2=all&txtarea0=these are the names of the children of Israel, which came into Egypt
*/
mysql_query("INSERT INTO findings (
book1,
chapter1,
verse1,
book2,
chapter2,
verse2,
txtarea0,
txtarea1,
txtarea2,
txtarea3,
txtarea4,
txtarea5,
comments
) VALUES (
'".$_GET['book1']."',
'".$_GET['chapter1']."',
'".$_GET['verse1']."',
'".$_GET['book2']."',
'".$_GET['chapter2']."',
'".$_GET['verse2']."',
'".$_GET['txtarea0']."',
'".$_GET['txtarea1']."',
'".$_GET['txtarea2']."',
'".$_GET['txtarea3']."',
'".$_GET['txtarea4']."',
'".$_GET['txtarea5']."',
'".$_GET['comments']."'
)");
echo $_GET['book1'].", ".$_GET['chapter1'].", ".$_GET['verse1'].", ".$_GET['book2'].", ".$_GET['chapter2'].", ".$_GET['verse2'].", ".$_GET['txtarea0'].", ".$_GET['txtarea1'].", ".$_GET['txtarea2'].", ".$_GET['txtarea3'].", ".$_GET['txtarea4'].", ".$_GET['txtarea5'].", ".$_GET['comments']." are inserted into the database!";
mysql_close($con);
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum