简体   繁体   中英

PHP prepared statement not returning array

My array is empty when I'm binding an id variable. The table contains 5 columns that I'd like in an array. This is what I tried:

$records = array();
$id = 22;

if($results = $db->prepare("SELECT * FROM categories WHERE id = ?")) {
$results->bind_param('i', $id);
$results->execute();
if($results->num_rows) {
    while($row = $results->fetch_object()) {
        $records[] = $row;
    }
    $results->free();
}
}

print_r($records);

you are binding param 'i' but using a '?' placeholder. Use one of these:

if($results = $db->query("SELECT * FROM categories WHERE id = ?")) {
$results->bind_param(1, $id);

or

if($results = $db->query("SELECT * FROM categories WHERE id = :i")) {
$results->bind_param(':i', $id);

See http://www.php.net/manual/en/pdostatement.bindparam.php for more examples.

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