简体   繁体   English

PDO没有创建CREATE TABLE

[英]PDO not creating CREATE TABLE

I'm pretty new to this and need some help. 我对此很陌生,需要一些帮助。

I am trying to input an array into a PDO database; 我正在尝试将数组输入到PDO数据库中; the database is created using this code: 使用以下代码创建数据库:

$createdb = $db->exec("CREATE DATABASE $validdbname;
CREATE USER '$user'@'localhost' IDENTIFIED BY '$password';
GRANT ALL ON `$validdbname` .* TO '$user'@'localhost';
FLUSH PRIVILEGES;")
or die(print_r($db->errorInfo(), true));
// Check for success
if ($createdb) 
{
    echo 'Database Created!<br /><br />';
}
else echo "Database not successfully created! <br /><br />";

This works successfully, however when I attempt to create a table within the created database with very similar code, this does not work: 这可以成功运行,但是当我尝试使用非常相似的代码在已创建的数据库中创建表时,此操作将无效:

$createtable = $db->exec("CREATE TABLE IF NOT EXISTS webdata (
id INTEGER NOT NULL AUTO_INCREMENT,
data VARCHAR(200) NOT NULL,
PRIMARY KEY (id))")

or die(print_r($db->errorInfo(), true));

I have checked on PHPMyAdmin to see if a table has been created, which while databases have been created the tables aren't. 我检查了PHPMyAdmin,以查看是否已创建表,而在创建数据库时未创建表。 This is the error that shows: 这是显示的错误:

Array ( [0] => 00000 [1] => [2] => )

I would be happy to give more code or information if necessary. 如有必要,我很乐意提供更多代码或信息。 I just cannot work out why this is happening and would greatly appreciate any input, thanks. 我只是无法弄清为什么会发生这种情况,非常感谢您的投入。

You need to 你需要

$db->exec('USE ' . $validdbname);

between the two queries, or use a fully qualified name in the CREATE TABLE : 在两个查询之间,或在CREATE TABLE使用完全限定名称:

CREATE TABLE IF NOT EXISTS $validdbname.webdata ( ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM