从mysql到mysqli更新代码

时间:2017-04-09 13:40:12

标签: mysql sql 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 个答案:

答案 0 :(得分:1)

  

mysqli_query()返回:

     

对于成功的SELECT,SHOW,DESCRIBE或EXPLAIN查询,它将返回一个mysqli_result对象。

     

对于其他成功的查询,它将返回TRUE。失败时错误

从您的错误中可以明显看出,您正在False获得$result,然后将mysqli_num_rows()传递给mysqli_result,其期望为$sql

如果productsproducts_description两个表中都存在相同的列名,则需要修改$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"; ,因为可能存在不明确的列错误。

/etc/apache2/sites-available