【问题标题】:Foreach and while loop not outputting my fetched dataForeach 和 while 循环不输出我获取的数据
【发布时间】:2015-10-13 03:58:34
【问题描述】:

我有以下两个准备好的语句。数据库连接和查询是正确的,我在 phpmyadmin 中测试过它们。我还在我的 while fetch 循环中进行了测试,看看我是否正在提取我应该是的数据并且我是。

问题在于我的 while 和 foreach 循环,或者可能是我的 num rows 语句。我不确定我在那里做错了什么。

我收到此错误:

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

对于这个while循环:

        while ($row2 = $result->fetch_assoc() ) {

我也得到了我的 else 声明..

    echo "<p>This topic does not exist.</p>";

即使信息正确回显,我还是认为我的循环是错误的?

有人看到我在循环中做错了什么吗?

$con = new mysqli("localhost", "", "", "");
if (mysqli_connect_errno()) {
    throw new Exception("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
    /* activate reporting */
$driver = new mysqli_driver();
try {
    $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->execute();
        $stmt->bind_result($topic_id, $category_id, $topic_title, $topic_creator, $topic_last_user, $topic_date, $topic_reply_date, $topic_views); 

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



while ($row = $stmt->fetch()) { 

     $stmt->store_result();
    $numrows = $stmt->num_rows;
    echo $numrows;
}
    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 `id`, `category_id`, `topic_id`, `post_creator`, `post_content`, `post_date` FROM forum_posts WHERE `category_id`=? AND `topic_id`=?")) {


            $stmt2->bind_param("ii", $cid, $tid);
            $stmt2->execute();
            $stmt2->bind_result($post_id, $post_category_id, $post_topic_id, $post_creator, $post_content, $post_date);

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

        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>";
        }
}
catch (Exception $e)
{
    echo "Error: " . $e->getMessage();
}

【问题讨论】:

  • 你是否将 mysql_query() 方法中的 SQL 查询作为字符串传递
  • 对不起,对 php 还是很陌生,但你到底是什么意思?我如何将它作为字符串传递。
  • like this mysql_query("select all from table");
  • 是的...if ($stmt = $con-&gt;prepare("SELECT * FROM forum_topics WHERE category_id=? AND id=? LIMIT 1")) {
  • 为什么不指定数据库名称。程序将从哪里获取数据

标签: php mysql foreach while-loop prepared-statement


【解决方案1】:

如果您在浏览器中使用 ctrl-F 搜索 $stmt(并忽略 $stmt2),您会注意到 $stmt 一直是 prepared statement 一直到错误行。 $stmt 作为来自prepare 的返回类型,被绑定并执行。

稍后再说:

if ($result = $con->query($stmt)) {

所以 $con->query() 期待的是一个字符串,而不是一个对象,不是吗?

来自manual

并不是说在显微镜下没有其他需要考虑的事情,但我希望这可以为您解答错误信息。

编辑

显然,您不能将 bind_result 与 select * 一起使用。阅读此问题的接受的男士答案。他做了2个例子,1个没有select *。另请注意store_result()

这是他的回答的link,得到了相当多的支持。

【讨论】:

  • 好的,我更改了该行以反映 $stmt2。我没有得到的部分是 numrows 部分。我知道它正在获取正确的 ID,因为我检查了它。所以我回应了$numrows,它说1,应该是。但由于某种原因,现在我不能让它回显 1,它说 0。但即使它回显 1,我也没有从第一个表中得到任何东西,上面写着添加回复或登录。我将编辑我的问题以显示更改。
  • 在制作$stmt$stmt2后我仍然得到那个对象错误?
  • 我知道看看 Edit @bottom
  • 我在第二个查询$stmt2-&gt;bind_param("ii", $cid, $tid); $stmt2-&gt;execute(); $stmt2-&gt;store_result(); $result2 = $stmt2-&gt;get_result(); 上测试了它,我得到了这个错误:Fatal error: Call to undefined method mysqli_stmt::get_result() i
  • 在你上面几行的评论中,这是一个使用select *的块......在这种情况下,那个人正在使用bind_result而你正在使用get_result ....更不用说你可能没有使用native driver
猜你喜欢
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 2018-05-20
  • 2013-10-01
  • 2016-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多