简体   繁体   中英

What's wrong with this simple PHP code?

I've just tried to start using PDO to handle database access in 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?

Note I followed this nettuts tutorial which gave an example without the method name.

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:

$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.

you missed the prepare function name

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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