在mysqli查询中获取致命错误


Getting fatal error on mysqli query

我有一个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;
        }
    }

我认为只要删除$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;
            }

?>

您的代码语法是正确的,只需在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();
?>