2014-11-20 30 views
0

我試圖從訂單表中打印出Product_Name和Order_Date,但不斷收到第35行的錯誤「num_rows線」。嘗試在內部連接語句上嘗試獲取非對象屬性的錯誤

<?php 
$servername = "localhost"; 
$username='root'; 
$password = ""; 
$dbname = "login"; 
?> 
<html><<html> 
    <head> 
     <title>Cart</title> 
     <link rel="stylesheet" href="tabMenu.css" type="text/css"> 
    </head> 
    <body> 

    </body> 
</html> 

<?php 
$mysqli = new mysqli($servername,$username, Null, $dbname); 
// Check connection 
if ($mysqli->connect_error) { 
die("Connection failed: " . $mysqli->connect_error); 
} 

session_start(); 

$results="Select orderline.Order_Date,p.Product_Name" 
     . "from orderline" 
     . "inner join product p" 
     . "on orderline.Product_ID=p.Product_ID"; 

$num=$mysqli->query($results); 



if ($results->num_rows) 
    { 
    while ($row=$results->fetch_object()) 
      { 
     echo "{$row->Order_Date} {$row->Product_ID} <br>"; 
      } 
    } 
    else 
    { 
    echo "No Results";} 

在此先感謝任何幫助將是偉大的。

回答

3

$resultsstring,字符串不是PHP中的對象(至少不是正常情況)。

你需要看看results objectnum_rows屬性:

$resultSet = $mysqli->query($results); 
$numRows = $resultSet->num_rows; 

if ($numRows > 0) { 
    while ($row = $resultSet->fetch_object()) { 
     echo "{$row->Order_Date} {$row->Product_ID} <br>"; 
    } 
} 

Here is some documentation

相關問題