简体   繁体   中英

How to Select Data with PHP PDO Prepared Statement

This is my PHP PDO Prepared Statement, to select and show data, but it is giving a lot of errors.

$sql = "SELECT * FROM `info` WHERE tag1 IN ('$tag1','$tag2') $andor tag2 IN ('$tag1','$tag2 ') ORDER BY $sort DESC LIMIT $limit OFFSET $start";
// prepare as you have
$sth = $conn->prepare($sql);
// Bind parameters while executing
$sth->execute(array(
':name'=>$name, 
':tag1'=>$tag1,
':tag2'=>$tag2
));
# Fetch the associate array
$result = $sth->fetch(PDO::FETCH_ASSOC);

for($x=0, $n=count($result); $x < $n; $x++){

echo "Name: ".$result[$x]['name']."    ";
}

Errors:

Notice: Undefined variable: movie_name

Notice: Undefined offset: 0

Everything is working fine, if I do not use prepared statements.

In order to bind the variables you need to change the query. Instead of "$tag1" use :tag1 and so on.

Regards

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