使用选择查询准备准备好的语句失败

时间:2015-07-20 06:15:19

标签: php mysql prepared-statement

我有以下准备好的声明在准备好的部分失败了。数据库表是正确的,表列也是如此。我在上面有一个类似的准备声明,工作得很好。有人在我的准备中看到了什么问题吗?

$stmt2 = $con->prepare("SELECT * FROM forum_posts WHERE `category_id`=? AND `topic_id`=?");

if ( !$stmt2 || $con->error ) {

    die('Select forum posts prepare() failed: ' . htmlspecialchars($con->error));

}

if(!$stmt2->bind_param('ii', $cid, $tid)) {

    die('Select forum posts bind_param() failed: ' . htmlspecialchars($stmt2->error));

}

if(!$stmt2->execute()) {

    die('Select forum posts execute() failed: ' . htmlspecialchars($stmt2->error));

}

更新

错误消息 - 选择论坛帖子prepare()失败:

所要求的完整代码:

使用Chris Brand的答案 - 它让我进入绑定参数并停在那里......

<?php
$con = mysqli_connect("localhost", "root", "", "db");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$cid = $_GET['cid'];
$tid = $_GET['tid'];
$userid = ( isset( $_SESSION['user'] ) ? $_SESSION['user'] : "" );

//Prepared SELECT stmt to get forum topics
$stmt = $con->prepare("SELECT * FROM forum_topics WHERE `category_id`=? AND id=? LIMIT 1");
if (!$stmt || $con->error ) {
    die('Select topics prepare() failed: ' . htmlspecialchars($con->error));
}
if(!$stmt->bind_param('ii', $cid, $tid)) {
    die('Select topics bind_param() failed: ' . htmlspecialchars($stmt->error));
}
if(!$stmt->execute()) {
    die('Select topics execute() failed: ' . htmlspecialchars($stmt->error));
}
$stmt->store_result();
$numrows = $stmt->num_rows;
if($numrows == 1){
    echo "<table width='100%'>";
    if ( $_SESSION['user'] ) { 
        echo "<tr><td colspan='2'><input type='submit' value='Add Reply' onClick=\"window.location = 
    'forum_post_reply.php?cid=".$cid."$tid=".$tid."'\"> <hr />";
    } else {
        echo "<tr><td colspan='2'><p>Please log in to add your reply</p><hr /></td></tr>";
    }

    foreach($stmt as $row) {

        //Prepared SELECT stmt to get forum posts
        if($stmt2 = $con->prepare("SELECT * FROM forum_posts WHERE `category_id`=? AND `topic_id`=?")) {
        //var_dump($stmt2);
        if (!$stmt2) {
            die('Select forum posts prepare() failed: ' . htmlspecialchars($con->error));
        }
        } else {
        var_dump($con->error);
        }
        if(!$stmt2->bind_param('ii', $cid, $tid)) {
            die('Select forum posts bind_param() failed: ' . htmlspecialchars($stmt2->error));
        }
        if(!$stmt2->execute()) {
            die('Select forum posts execute() failed: ' . htmlspecialchars($stmt2->error));
        }
        //while($row2 = mysqli_fetch_assoc($stmt2)){
        foreach($stmt2 as $row2) {  
            echo "<tr><td valign='top' style='border: 1px solid #000000;'>
            <div style='min-height: 125px;'>".$row['topic_title']."<br />
            by ".$row2['post_creator']." - " .$row2['post_date']. "<hr />" . $row2['post_content'] ."</div></td>
            <td width='200' valign='top' align='center' style='border: 1px solid #000000;'>User Info Here!</td></tr>
            <tr><td colspan='2'><hr /></td></tr>";
        }
    }
} else {
    echo "<p>This topic does not exist.</p>";
}

使用阿卜杜拉的回答:

$con = mysqli_connect("localhost", "root", "", "db");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$cid = $_GET['cid'];
$tid = $_GET['tid'];
$userid = ( isset( $_SESSION['user'] ) ? $_SESSION['user'] : "" );

//Prepared SELECT stmt to get forum topics
$stmt = $con->prepare("SELECT * FROM forum_topics WHERE `category_id`=? AND id=? LIMIT 1");
if (!$stmt || $con->error ) {
    die('Select topics prepare() failed: ' . htmlspecialchars($con->error));
}
if(!$stmt->bind_param('ii', $cid, $tid)) {
    die('Select topics bind_param() failed: ' . htmlspecialchars($stmt->error));
}
if(!$stmt->execute()) {
    die('Select topics execute() failed: ' . htmlspecialchars($stmt->error));
}
$stmt->store_result();
$numrows = $stmt->num_rows;
if($numrows == 1){
    echo "<table width='100%'>";
    if ( $_SESSION['user'] ) { 
        echo "<tr><td colspan='2'><input type='submit' value='Add Reply' onClick=\"window.location = 
    'forum_post_reply.php?cid=".$cid."$tid=".$tid."'\"> <hr />";
    } else {
        echo "<tr><td colspan='2'><p>Please log in to add your reply</p><hr /></td></tr>";
    }

    foreach($stmt as $row) {

            //Prepared SELECT stmt to get forum posts
        $stmt2 = $mysqli::prepare("SELECT * FROM forum_posts WHERE category_id='value' AND topic_id='value'");
        //var_dump($stmt2);
        //if ( !$stmt2 || $con->error ) {
            //die('Select forum posts prepare() failed: ' . htmlspecialchars($con->error));
    //  }
        //var_dump($con->error);
        if(!mysqli_stmt_bind_param($stmt2,"i", $cid, $tid)) //i means intiger
        {
            die('Select forum posts bind_param() failed: ' . htmlspecialchars($stmt2->error));
        }
        if(!mysqli_stmt_execute($stmt2)) {
            die('Select forum posts execute() failed: ' . htmlspecialchars($stmt2->error));
        }
        //while($row2 = mysqli_fetch_assoc($stmt2)){
        foreach($stmt2 as $row2) {  
            echo "<tr><td valign='top' style='border: 1px solid #000000;'>
            <div style='min-height: 125px;'>".$row['topic_title']."<br />
            by ".$row2['post_creator']." - " .$row2['post_date']. "<hr />" . $row2['post_content'] ."</div></td>
            <td width='200' valign='top' align='center' style='border: 1px solid #000000;'>User Info Here!</td></tr>
            <tr><td colspan='2'><hr /></td></tr>";
        }
    }
} else {
    echo "<p>This topic does not exist.</p>";
}

3 个答案:

答案 0 :(得分:0)

Prepared Statements因以下几个原因失败:

1)数据库未连接。

2)未正确构建查询

/* First check the correction */
    if ( mysqli_connect_errno() ) {
        printf("Connect failed: %s\n", mysqli_connect_error());
    }

/* Secondly try to print out the query after this statement*/

$stmt2 = $con->prepare("SELECT * FROM forum_posts WHERE `category_id`=? AND `topic_id`=?");
var_dump($stmt2);

我的猜测是$stmt2正在返回FALSE

答案 1 :(得分:0)

$con->error是一个字符串,它可能导致你的if语句评估为true。

尝试类似:

$con->free();

if (!$stmt2 || !empty($con->error))

$con->free();

if ($stmt2 = $con->prepare(...))
{
   ...
}
else
{
    var_dump($con->error);
}

如果填充了您的语句对象(如另一个答案所示),则它必须是$con->error变量,其值为true

更新

您应该尝试使用http://php.net/manual/ru/mysqli-result.free.phphttp://www.php.net/manual/ru/mysqli-stmt.close.php发布上一个语句。

答案 2 :(得分:0)

使用此

$stmt2 = $mysqli->prepare("SELECT * FROM forum_posts WHERE category_id='value' AND topic_id='value'")

//检查连接错误

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

//绑定参数值

read this table

if(!mysqli_stmt_bind_param($stmt2,"i", $cid, $tid)) //i means intiger
{
    die('Select forum posts bind_param() failed: ' . htmlspecialchars($stmt2->error));
}

image

if(!mysqli_stmt_execute($stmt2)) 
{

    die('Select forum posts execute() failed: ' . htmlspecialchars($stmt2->error));

}