...

View Full Version : Resolved test for table created script



sonny
10-25-2012, 09:12 PM
Hi trying to create a simple setup script
How do you test, if the table actually
gets created or not?



if($_POST['setup'] == 1) {
// connect...

$table = $_POST['prefix']."_MyTable";
mysql_query("CREATE TABLE $table (
id int(5) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
test INT(11) NOT NULL)");
Print "The table has been created";}


Thanks
Sonny

Fou-Lu
10-25-2012, 10:48 PM
You would check the result of mysql_query. True would be successful.

sonny
10-25-2012, 10:59 PM
You would check the result of mysql_query. True would be successful.

Hi
Thought I tried something like that, ,but does not work, way you mention if not the same
do not know how to implement that correctly


$tableOK = mysql_query("show tables like '$table'");
if ($tableOK === true) echo "Table Created";
else echo "ERROR";


Thanks
Sonny

Fou-Lu
10-25-2012, 11:04 PM
"show" returns a resultset; you cannot compare it identically to true. You need to check the results of the create table SQL.

sonny
10-25-2012, 11:23 PM
"show" returns a resultset; you cannot compare it identically to true. You need to check the results of the create table SQL.

Hi does this look right? is this what you mean


$table = $_POST['prefix']."_MyTable";
$queryOK = mysql_query("CREATE TABLE IF NOT EXISTS $table (
id int(5) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
test INT(11) NOT NULL)");

if ($queryOK === true) echo "Table Created";
else echo "ERROR Table Not Created";}

Fou-Lu
10-26-2012, 12:00 AM
Yep, that should do it. The docs don't specifically say that CREATE will return a true/false, but they do indicate that DROP does, so that would implicitly tell me that create does as well.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum