PHP代码,(数据库,论坛)错误

时间:2013-07-14 13:24:36

标签: php database forum

需要帮助我的PHP代码,我收到此错误:

  

警告:mysqli_stmt :: bind_param()[mysqli-stmt.bind-param]:变量数量与第18行C:\ wamp \ www \ test \ forum.php中预准备语句中的参数数量不匹配

这是forum.php:

<?php
session_start();
require"db_connect.php";
//get the page id
if(isset($_GET['id'])&&is_numeric($_GET['id'])){
        $id = $_GET['id'];
} else {
    die("Error");
}
//check
$idCheck = $db->query("SELECT * FROM forum_tabl WHERE forum_id = '$id'");
if($idCheck->num_rows !==1){
    die("error");
}
$row = $idCheck->fetch_object();
$sql = "SELECT post_id, post_title FROM forum_post WHERE forum_id=1 AND post_type='o'";
if($query = $db->prepare($sql)){
    $query->bind_param('s',$id);
    $query->bind_result($post_id, $post_title);
    $query->execute();
    $query->store_result();
}else{
    echo $db->error;
}

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $row->forum_name?></title>
</head>

<body>
<div id="container">
    <table width="80%">
        <?php if($query->num_rows!=0):?>
        <?php while ($query->fetch()):?>
        <tr>
            <td><?php echo $post_title?></td>
        </tr>
        <?php endwhile;?>
        <?php else:?>
        <tr>
            <td><h2>No Posts Found</h2></td>
        </tr>
        <?php endif;?>
    </table>
</div>
</body>
</html>


这就是它现在的样子:http://wildmine.tk/test/forum.php?id=1

1 个答案:

答案 0 :(得分:2)

$sql = "SELECT post_id, post_title FROM forum_post WHERE forum_id=1 AND post_type='o'";

应该是

$sql = "SELECT post_id, post_title FROM forum_post WHERE forum_id= ? AND post_type='o'";

现在,您将参数绑定到此查询。