问题描述
我在排查一些简单的PHP代码以在MySQL表中插入记录时遇到困难.
I am having difficulties troubleshooting some simple PHP code to insert a record in a MySQL table.
直接在WAMP中输入此代码可以正常工作:
This code entered directly into WAMP works fine:
INSERT INTO `users` (`userName`,`userEmail`) VALUES ('orange','orange@gmail.com')
此PHP代码不起作用:
This PHP code doesn't work:
<?php
$dbHost="localhost";
$dbName="project";
$dbUser="admin";
$dbPassword="abcd";
$dbh=new PDO("mysql:host=$dbHost;dbName=$dbName", $dbUser, $dbPassword);
print_r($dbh);
echo "</br>";
print_r($dbh->errorInfo());
$query=$dbh->prepare("INSERT INTO users (userName, userEmail) VALUES (?,?)");
echo "</br>";
print_r(var_dump($query->errorInfo()));
echo "</br>";
print_r($query->errorCode());
echo "</br>";
print_r($dbh->errorInfo());
$query->bindValue(1, 'apple');
echo "</br>";
print_r(var_dump($query->errorInfo()));
echo "</br>";
print_r($query->errorCode());
echo "</br>";
print_r($dbh->errorInfo());
$query->bindValue(2, 'apple@gmail.com');
echo "</br>";
print_r(var_dump($query->errorInfo()));
echo "</br>";
print_r($query->errorCode());
echo "</br>";
print_r($dbh->errorInfo());
$inserted=$query->execute(); //True if succesful, False if not.
echo "</br>";
print_r(var_dump($query->errorInfo()));
echo "</br>";
print_r($query->errorCode());
echo "</br>";
print_r($dbh->errorInfo());
echo "</br>";
if ($inserted){print_r("true");}else{print_r("false");};
?>
执行页面时得到的是以下打印输出:
What I get when I execute the page is the following printout:
PDO Object ( )
Array ( [0] => [1] => [2] => )
array(3) { [0]=> string(0) "" [1]=> NULL [2]=> NULL }
Array ( [0] => 00000 [1] => [2] => )
array(3) { [0]=> string(0) "" [1]=> NULL [2]=> NULL }
Array ( [0] => 00000 [1] => [2] => )
array(3) { [0]=> string(0) "" [1]=> NULL [2]=> NULL }
Array ( [0] => 00000 [1] => [2] => )
array(3) { [0]=> string(5) "3D000" [1]=> int(1046) [2]=> string(20) "No database selected" }
3D000
Array ( [0] => 00000 [1] => [2] => )
false
记录未插入数据库中.我做错了什么?我不确定在print_r的内容中应该看到什么,我是否将它们作为对响应者的帮助.
The record isn't inserted in the db. What I am doing wrong? I am not sure what I should see in the print_r's, I am providing them as help for responders.
谢谢
JDelage
已编辑-我在注释中添加了print_r的推荐内容.
edited - I added the print_r's recommended in the comments.
这是我在WAMP中看到的:
Here is what I see in WAMP:
推荐答案
该错误消息似乎表明您已经连接到数据库,但是尚未选择项目数据库.
The error message seems to indicate that you've connected to the DB fine, but that the project database hasn't been selected.
为确保它正在尝试使用正确的DSN进行纠正,我会尝试将连接字符串更改为直接包含值而不是变量,即:
To be sure it's trying to correct with the right DSN, I'd try changing the connection string to contain values directly, rather than variables, i.e.:
'mysql:host=localhost;dbname=project'
这应该没有什么区别,但是值得检查.
This shouldn't make a difference, but it's worth checking.
如果这不起作用,并且由于您似乎能够连接到MySQL,则解决方法可能是将数据库名称包含在查询中.因此,您上面的查询将变为:
If that doesn't work, and since you appear to be able to connect to MySQL, a workaround could be to include the database name as part of the query. So your query above would become:
$query=$dbh->prepare("INSERT INTO project.users (userName, userEmail) VALUES (?,?)");
这篇关于在MySQL db中插入的PHP PDO代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!