2017-04-09 60 views
0

我想更新舊腳本,如下所示。從MySQL到mysqli更新代碼

mysql sql statement that works is: 
$result = mysql_query("SELECT products.products_id AS id, 
products_description.products_name AS name, 
products.products_quantity AS quantity, products.products_weight AS weight, 
products.products_price AS price 
FROM products, products_description WHERE 
products.products_id=products_description.products_id;") or 
die(mysql_error()); 

while($row = mysql_fetch_array($result)) { 

的mysqli的聲明,我不能去工作是:

$sql = "SELECT products_id, products_description.products_name, 
products_quantity, products_weight, products_price FROM products INNER JOIN 
products_description on products.products_id = 
products_description.products_id"; 

$result = mysqli_query($con, $sql); 
if(mysqli_num_rows($result)>0){ 


while(null !== ($row = mysqli_fetch_assoc($result))) { 

我收到的錯誤是:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean 
given in index.php on line 19 

和mysqli聲明工作,但不是當我添加products_description.products_name來查詢。

回答

1

mysqli_query()返回:

對於成功的SELECT,SHOW,DESCRIBE,或解釋的查詢將返回一個mysqli_result對象。

對於其他成功的查詢,它將返回TRUE。 FALSE的失敗

可以清楚地看出,從你的錯誤,你得到False$result你,然後傳遞mysqli_num_rows(),其中預計爲mysqli_result

如果列表productsproducts_description中存在相同的列名稱,則需要修改$sql,因爲可能存在模糊的列錯誤。

$sql = "SELECT p.products_id, pd.products_name, p.products_quantity, p.products_weight, p.products_price 
FROM products p 
INNER JOIN products_description pd on p.products_id = pd.products_id"; 
+0

工作就像一個夢想satish!學習如何在PDO中做到這一點 – craig

+0

很高興知道。也許這也將是有益的:https://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059 –