使用prepare语句查询数据库以从数据库中获取ID

时间:2018-04-05 20:23:37

标签: php mysql prepared-statement

我是PHP的新手,还没有掌握准备语句。当前语句不断给我这个错误:Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given这可能意味着select语句为false。有人可以帮我把这个陈述改成准备陈述吗?   这是带有问题的代码(user_account.php)

<?php
require('db.php');

session_start();


$id = mysqli_real_escape_string($con, $_GET['mentorID']);
$result=mysqli_query($con, "SELECT * FROM logged_cat LEFT JOIN category ON category.catID = logged_cat.catID LEFT JOIN mentor ON mentor.mentorID = logged_cat.mentorID WHERE mentorID = $id");

?>
<?php

echo "<table cellspacing='0'>

        <tr>
            <th>Name</th>
             <th>Email</th>
             <th>Gender</th>
             <th width='280'>Bio</th>
        </tr>";
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

    echo "<tr>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['email'] . "</td>";
    echo "<td>" . $row['gender'] . "</td>";
    echo "<td>" . $row['bio'] . "</td>";
    echo "</tr>";
}
echo "</table>";
?>

这是它的前驱脚本,它运行得非常好:

<?php
require('db.php');

session_start();

$mysqli = new mysqli('localhost','root','', 'ymp');

$stmt = $mysqli->prepare("SELECT * FROM logged_cat LEFT JOIN category ON category.catID = logged_cat.catID LEFT JOIN mentor ON mentor.mentorID = logged_cat.mentorID WHERE catName LIKE ?");
$stmt->bind_param('s', $_GET['catName']);
$stmt->execute();

$res = $stmt->get_result();
$row = $res->num_rows;


?>
<div class="table-users">
    <div class="header">Mentors</div>
    <?php $id = $row['mentorID']; ?>
<?php

echo "<table cellspacing='0'>

        <tr>
            <th>Name</th>
            <th>Email</th>
            <th width='280'>Bio</th>
        </tr>";
while($row = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
    $id = $row['mentorID'];
    $name = $row['name'];

    echo "<tr>";
    echo ('<td><a href="user_account.php?mentorID=' . $id . '">' . $name . '</a></td>');
    echo "<td>" . $row['email'] . "</td>";
    echo "<td>" . $row['bio'] . "</td>";
    echo "</tr>";
}
echo "</table>";
?>

0 个答案:

没有答案