PDO无法在Wamp服务器上的php中工作
问题描述:
我面临使用PDO将数据导入数据库的困难.插入在我使用mysql_connect
/mysql_query
时有效,所以我确定这与PDO有关,我做的不好:
I am facing difficulty getting data into my database using PDO. The insertion works when I use mysql_connect
/mysql_query
though, so I am sure it's something about PDO I am not getting right:
$db = new PDO("mysql:host=localhost;dbname=XXXXXX","root","");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try{
$query= "INSERT INTO tableC VALUES ('1111','2222','XX YY','ERTY','33');";
$result = $conn ->prepare($query);
$result ->execute();
} catch {
echo "PDO error:" . $exception->getMessage();
}
要点:我正在使用PHP并连接到WAMP服务器. PHP版本是5.3.13.我还检查了我的查询是否有效.
Couple of points: I am using PHP and connecting to WAMP server. PHP version is 5.3.13. I have also checked that my query works.
答
您正在使用$conn->
,就像使用$db
进行连接一样,因此下面的代码段
You are using $conn->
where as you are using $db
for connection so the below snippet
$conn ->prepare($query);
应写为
$db->prepare($query);
此外,您从哪里获得的$exception
?您的catch
语句应类似于
Also, where did you got $exception
from? Your catch
statement should look like
catch(PDOException $exception) {
}