在准备好的陈述中显示num_rows和fetch assoc

时间:2016-10-27 13:38:03

标签: php mysqli prepared-statement

我多次尝试此查询。但是,它仍然是错误的。我糊涂了 。请帮帮忙:

<?php
$sql = $db->prepare("SELECT nm_bahan FROM bahan WHERE nm_bahan=? ");
        $sql->bind_param("s", $_POST['nm_bahan']);
        $sql->execute();
        $sql->store_result();
        $jml = $sql->num_rows;
        
        $result = $sql->get_result();
        $data = $result->fetch_assoc();

echo $jml . "<br>" . $data['nm_bahan'];

?>

1 个答案:

答案 0 :(得分:1)

您的数据库只返回一个结果集。所以你不能两次。

<?php
$sql = $db->prepare("SELECT nm_bahan FROM bahan WHERE nm_bahan=? ");
$sql->bind_param("s", $_POST['nm_bahan']);
$sql->execute();
$result = $sql->get_result();
$data = $result->fetch_assoc();
// I have no idea why you need this useless number but meh
$jml = $result->num_rows;

echo $data['nm_bahan'];