简体   繁体   English

这个简单的PHP代码有什么问题?

[英]What's wrong with this simple PHP code?

I've just tried to start using PDO to handle database access in PHP. 我刚刚尝试开始使用PDO处理PHP中的数据库访问。

I tried the following code: 我尝试了以下代码:

$dbh = new PDO("mysql:host=$kdbhost;dbname=$kdbname",$kdbuser,$kdbpw);
$sth = $dbh->("INSERT INTO enquiries (name, email, message) VALUES(:name, :email, :message);");

And dreamweaver gives me a syntax error on the second line, and I can't figure out for the life of me why? 而且Dreamweaver在第二行给了我一个语法错误,而我一生都无法弄清楚为什么?

Note I followed this nettuts tutorial which gave an example without the method name. 注意,我遵循了这个nettuts教程该教程给出了一个没有方法名称的示例。

You need to do this: 您需要这样做:

$sth = $dbh->prepare("INSERT INTO enquiries (name, email, message) VALUES(:name, :email, :message);");

$dbh->( is a syntax error because you're not calling a method of the $dbh object. In this case, you want to use prepare() to prepare the query, so you end up with: $dbh->(是语法错误,因为您没有调用$dbh对象的方法。在这种情况下,您想使用prepare()来准备查询,因此最终得到:

$sth = $dbh->prepare( ... );

You are missing a method in your call: 您在呼叫中缺少一种方法:

$sth = $dbh->yourMethod("INS...

Replace yourMethod with prepare (if that's the method you need) or any other method provided from the PDO instance. 用prepare(如果需要的话)或PDO实例提供的任何其他方法替换yourMethod。

you missed the prepare function name 您错过了准备功能名称

$sth = $dbh-> prepare ("INSERT INTO enquiries (name, email, message) VALUES(:name, :email, :message);"); $sth = $dbh-> prepare ("INSERT INTO enquiries (name, email, message) VALUES(:name, :email, :message);");

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

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