【问题标题】:Issues outputting data after prepared statement with loops在使用循环准备好的语句后输出数据的问题
【发布时间】:2015-10-12 18:37:34
【问题描述】:

在下面的代码中,我很难弄清楚我在 while 和 foreach 循环中做错了什么。我倾向于混合面向对象和过程 mqsqli,但每次我认为我做对了,我都会得到一个错误。

我在这段代码中的循环中做错了什么?

现在我收到此错误

Warning: mysqli::query() expects parameter 1 to be string, 

完整代码

try {
$con = new mysqli("localhost", "", "", "");
if (mysqli_connect_errno()) {
    throw new Exception("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$cid = $_GET['cid'];
$tid = $_GET['tid'];
$userid = ( isset( $_SESSION['user'] ) ? $_SESSION['user'] : "" );
    echo $cid . "<br>";
    echo $tid;
//Prepare
if ($stmt = $con->prepare("SELECT * FROM forum_topics WHERE `category_id`=? AND `id`=? LIMIT 1")) {

    $stmt->bind_param("ii", $cid, $tid);
    //$stmt->fetch();

    if (!$stmt) {
        throw new Exception($con->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);

        $stmt2->bind_param("ii", $cid, $tid);
        $stmt2->execute();
    }   
    if ($result = $con->query($stmt)) {
        while ($row2 = $result->fetch_assoc() ) {
            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>";
    }

【问题讨论】:

  • 对不起,这里复制时忘记执行部分。它有什么问题?

标签: php mysql foreach while-loop


【解决方案1】:
if ($stmt = $con->prepare("SELECT * FROM forum_topics WHERE `category_id`=? AND `id`=? LIMIT 1")) {

$stmt->bind_param("ii", $cid, $tid);
//$stmt->fetch();

if (!$stmt) {
    throw new Exception($con->error);
}
}

    $stmt->store_result();
    $numrows = $stmt->num_rows;

在循环遍历foreach($stmt as $row) { 之前,您实际上不会执行$stmt

你会想把它扔进去:

$stmt-&gt;execute()

你的逻辑有点过头了。您看起来像是在循环遍历从未执行过的查询结果,然后您尝试在此处重新查询原始 $stmt

if ($result = $con-&gt;query($stmt)) {

编辑:与您聊天后,您需要编辑原始查询以查询特定列,以便您可以像这样引用它们:

if ($stmt = $con->prepare("SELECT topic_creator FROM forum_topics WHERE `category_id`=? AND `id`=? LIMIT 1")) {

...

$stmt->bind_result($topic_creator); 
while ($stmt->fetch()) { 
   echo "TC: " . $topic_creator . "<br>"; 
}

您可以将其应用于while 循环中的子查询。

【讨论】:

  • 我实际上是意外评论了这一点。当执行在其中时,它仍然给出相同的错误。那应该是什么?我真的对此感到困惑,无法弄清楚我在哪里搞砸了。
  • 您正在准备您的$stmt,但从未实际执行查询。您需要执行以下操作:pastebin.com/GRusBAfC
  • 另外,请确保您的查询是正确的,并且您的 $_GET 变量正在正确地填充您的绑定变量。
  • 我正在执行查询。我知道我的问题在于我的 fetches、while 循环和 foreach。我只是不知道我对他们做错了什么。这是我的代码,其中包含执行,我只是在提问时忘记包含它。 $stmt-&gt;bind_param("ii", $cid, $tid); $stmt-&gt;execute();
  • 我准备好的声明检查正常。 $cid 和 $tid 的值很好,我只是不知道如何获取和循环结果。
猜你喜欢
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
  • 2011-07-13
  • 2014-06-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多