在mysqli查询上出现致命错误

时间:2014-09-06 22:31:49

标签: php mysqli

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

2 个答案:

答案 0 :(得分:1)

我认为这应该只是删除$ result = $ con&gt; 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;
            }

&GT;

答案 1 :(得分:0)

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