1

Here is my code:

$STH1000=$DBH->query("CREATE TABLE IF NOT EXISTS '".$dept.$year."' ( `ID` INT( 255 ) NOT NULL AUTO_INCREMENT , `URL` VARCHAR( 255 ) NOT NULL , PRIMARY KEY ( `ID` )) ") or die "Could not prepare sql statement";

All I'm trying to do is create a table if $dept$year doesn't exist but the way I have it now, it throws errors.

I believe there is a way to create a table is mysql returns an error on a select but I'm not sure how to do that either. Any help would be greatly appreciated.

4

2 回答 2

5
$STH1000=$DBH->query("CREATE TABLE IF NOT EXISTS ".$dept.$year." ( `ID` INT( 255 ) NOT NULL AUTO_INCREMENT , `URL` VARCHAR( 255 ) NOT NULL , PRIMARY KEY ( `ID` )) ") or die "Could not prepare sql statement";

表名不得关闭''(但我可以选择关闭``)。

于 2012-12-13T20:52:33.247 回答
1

'".$dept.$year."'只需从as中删除引号".$dept.$year."

您的查询将是这样的

$STH1000=$DBH->query("CREATE TABLE IF NOT EXISTS ".$dept.$year." ( `ID` INT( 255 ) NOT NULL AUTO_INCREMENT , `URL` VARCHAR( 255 ) NOT NULL , PRIMARY KEY ( `ID` )) ") or die "Could not prepare sql statement";
于 2012-12-13T20:53:50.597 回答