【问题标题】:Getting fatal error on mysqli querymysqli查询出现致命错误
【发布时间】:2014-09-06 22:31:49
【问题描述】:

我有一个 mysqli 查询,当我运行时返回以下错误。

致命错误:在第 57 行的 /home/tkweb/public_html/intermate.eu/companionsearch.php 中的非对象上调用成员函数 fetch_assoc()

我不确定为什么会出现此错误,因为我使用完全相同的代码运行查询,但实际查询参数和输出本身在另一个工作正常的页面上。

这是返回错误的代码:

$query2 = $con->query("SELECT * FROM journeys WHERE origin = $origin AND destination =   $destination AND date = $date AND hour = $hour AND minute = $minute AND id != $id");
$count = mysqli_num_rows($query2);

if($count = 0) {
    echo "Sorry, there are no other InterRailer's making this journey, try searching for individual legs if your journey contains more than one leg.";
}else{
    $result = $con->query($query2);
    while ( $row = $result->fetch_assoc() ) {
        $companion[] = "<form id='companionresult' method='post' action='accountview.php'><input type='hidden' name='id' value='{$row['id']}'>{$row['firstname']}, {$row['age']}<br>{$row['nationality']}<br>Speaks: {$row['language1']}{$row['language2']}{$row['language3']}{$row['language4']}{$row['language5']}<br>  <input id='submit3' type='submit' name='submit' value='View Profile'><br>";
        foreach($companion as $info)
            echo $info;
        }
    }

【问题讨论】:

  • 学习使用var_dump()

标签: php mysqli


【解决方案1】:

我认为这应该只是删除 $result = $con->query($query2);

              $query2 = $con->query("SELECT * FROM journeys WHERE origin = $origin AND destination =   $destination AND date = $date AND hour = $hour AND minute = $minute AND id != $id");
              $count = mysqli_num_rows($query2);

              if($count = 0) {
                echo "Sorry, there are no other InterRailer's making this journey, try searching for individual legs if your journey contains more than one leg.";
              }
              else{
                //$result = $con->query($query2);
                while ( $row = $query2->fetch_assoc() ) {

                $companion[] = "<form id='companionresult' method='post' action='accountview.php'><input type='hidden' name='id' value='{$row['id']}'>{$row['firstname']}, {$row['age']}<br>{$row['nationality']}<br>Speaks: {$row['language1']}{$row['language2']}{$row['language3']}{$row['language4']}{$row['language5']}<br>  <input id='submit3' type='submit' name='submit' value='View Profile'><br>";

                foreach($companion as $info)
                echo $info;
                }
              }

编辑:实际上问题出在您的查询中,所以打印您的查询并在 phpmyadmin 或 sql 中运行它,看看是否出现任何错误。这是我尝试过的,它在我的电脑上运行。

    <?php $con=  mysqli_connect("localhost", "root", "admin","demo");
        $id=2; 
        $origin='india';
        $destination='delhi';
        $date='2014-09-23';
        $hour='1';
        $minute='10';

    //for better error detection print your query here
           /*echo "SELECT * FROM journeys WHERE origin = '$origin' 
                 AND destination = '$destination' AND date ='$date' AND hour ='$hour' 
                AND minute ='$minute' AND id !='$id'";*/

        //Quote values in single quote for string or date values because if they will be blank your query will go wrong, it will mix with and like where origin= AND destination= which will produce error.
        $query2 = $con->query("SELECT * FROM journeys WHERE origin = '$origin' 
                 AND destination = '$destination' AND date ='$date' AND hour ='$hour' 
                AND minute ='$minute' AND id !='$id'");

          $count = mysqli_num_rows($query2);

          $companion=array();
          if($count = 0) {
            echo "Sorry, there are no other InterRailer's making this journey, try searching for individual legs if your journey contains more than one leg.";
          }
          else{
            //$result = $con->query($query2);
            while ( $row = $query2->fetch_assoc() ) {
            $companion[] = "<form id='companionresult' method='post' action='accountview.php'><input type='hidden' name='id' value='{$row['id']}'>{$row['firstname']}, {$row['age']}<br>{$row['nationality']}<br>Speaks: {$row['language1']}{$row['language2']}{$row['language3']}{$row['language4']}{$row['language5']}<br>  <input id='submit3' type='submit' name='submit' value='View Profile'><br>";
          }
          //print your foreach outside while loop.
           foreach($companion as $info)
                echo $info;
            }

?>

【讨论】:

  • 仔细看,RN Kushwaha 没有这么说,但他还把下一行改成了while ( $row = $query2-&gt;fetch_assoc() ) {。请注意将$Result 更改为$query2。他可能是出于习惯而忘记了。
  • 没注意到。我试了一下,但它仍然给我同样的错误。
  • 查看我上面的编辑,如果发生任何错误,请始终打印您的查询并运行到您的 phpmyadmin。
【解决方案2】:

您的代码语法是正确的,只需通过在 phpmyadmin 中执行来检查您的查询。 我认为查询的问题。 这是 fetch_assoc() 函数的示例

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";    
if ($result = $mysqli->query($query)) {

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
    }

    /* free result set */
    $result->free();
}
/* close connection */
$mysqli->close();
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-20
    • 1970-01-01
    相关资源
    最近更新 更多