0

I am trying to update an old script as seen below.

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 )) {

The mysqli statements that I can't get to work is:

$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))) {

The error I receive is:

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

the mysqli statement worked but not when I add the products_description.products_name to the query.

Dharman
  • 30,962
  • 25
  • 85
  • 135
craig
  • 45
  • 1
  • 7

1 Answers1

1

mysqli_query() returns :

For successful SELECT, SHOW, DESCRIBE, or EXPLAIN queries it will return a mysqli_result object.

For other successful queries it will return TRUE. FALSE on failure

As evident from your error, you are getting False as $result which you are then passing in mysqli_num_rows(), which expects as mysqli_result.

You need to modify your $sql as following as there may be ambiguous column error, if same column names exists in both tables products and products_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";
Community
  • 1
  • 1
Satish Prakash Garg
  • 2,213
  • 2
  • 16
  • 25