当前位置: 首页 > 知识库问答 >
问题:

prepared语句的准备与select查询失败

松旭
2023-03-14
$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));

}

使用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>";
}

共有1个答案

柴丰
2023-03-14

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);
 类似资料:
  • 我们有一个db2 database V9.7FP1 出现此错误消息的可能原因是什么?

  • 我有一个搜索查询,它必须使用包含搜索搜索表中的一列。列上有CTXSYS.Context类型索引。当使用prepared语句在表中获取数据时,搜索查询不能处理像-、/、_等特殊字符。

  • 问题内容: 我总是发现很难编写MySQLi预备语句,因为许多函数的工作方式与旧方法不同。现在我面临一个问题。 问题答案: 您正在尝试通过以下方式获取结果 事实并非如此。因为execute将仅返回布尔值。 做喜欢的。

  • 我有一个类,其中我使用旧的jdbc方法创建了到h2数据库的连接,该方法编写url并获取连接,我在数据库中创建了一个表,这个表不是java对象,所以我用尽了聪明的方法来为我的方法编写测试 //我还创建了一个表app_user公共void addUser(连接连接,字符串登录,字符串密码,字符串描述)抛出SQLException{ 而我的测试方法看起来像这样,有没有办法让它不那么整洁,更简单,我试图用

  • 我有以下准备的一个准备好的声明失败了... 我在我的第一份准备好的声明中几乎有同样的准备,而且它进行得很好。我不确定是否我有两个准备好的声明,如果这是造成一个问题,或情况可能是什么? 现在我有个错误... 更新-可能的内部连接,像这样?