简体   繁体   English

为什么此PHP / SQL无法创建表?

[英]Why won't this PHP/SQL create the table?

<?php
try {
  $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
  $sql = $DBH->exec("
    CREATE TABLE `store_config` (
    store_name varchar(30),
    last_update date)
    ");
} catch (PDOException $e) {
  echo "Could not connect to database.";
  $DBH = null;
}
?>

The PDOException is not thrown, there is probably an SQL error but I don't know how to retrieve that error and when I check the database with phpMyAdmin there are no tables created. 没有抛出PDOException,可能是一个SQL错误,但是我不知道如何检索该错误,当我使用phpMyAdmin检查数据库时,没有创建任何表。 What did I do wrong? 我做错了什么?

Thanks for reading. 谢谢阅读。

EDIT: When I executed the same SQL code directly into phpMyAdmin it gave permission denied error... 编辑:当我直接执行相同的SQL代码到phpMyAdmin中时,它给出了权限被拒绝的错误...

1142 - CREATE command denied to user '$user'@'$my_ip' for table 'store_config' 1142-表'store_config'的用户'$ user'@'$ my_ip'的CREATE命令被拒绝

I am confused because when I created the user with the web hosts web form it didn't give me options to grant any permissions but only to make this user the database owner - is that what I have to do? 我很困惑,因为当我使用Web主机Web表单创建用户时,它没有给我授予任何权限的选项,而只是让该用户成为数据库所有者-这就是我要做的吗?

EDIT: 编辑:

I gave my user account 'DBO access' and it works now (the table is created). 我为用户帐户授予了“ DBO访问权限”,并且该帐户现在可以正常工作(表已创建)。

Thanks for your comments. 感谢您的意见。

Try this. 尝试这个。 It should get you the error you're looking for that will tell you whats going on. 它应该使您得到正在寻找的错误,它将告诉您发生了什么。

<?php
try {
  $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
  $sql = $DBH->exec("
    CREATE TABLE `store_config` (
    store_name varchar(30),
    last_update date)
    ") or die(print_r($DBH->errorInfo(), true));
} catch (PDOException $e) {
  echo "Could not connect to database.";
  $DBH = null;
}
?>

You need to find a way to grant the create permission to your user from the machine you're running the code from. 您需要找到一种从运行代码的计算机上向用户授予创建权限的方法。 The user probably only has permissions from localhost, so you'd have to upload the code to the server and run it there to have permission. 该用户可能仅具有来自本地主机的权限,因此您必须将代码上传到服务器并在该服务器上运行以具有权限。

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

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