0

My simplified code is:

<?php
$con = mysql_connect("aaa", "bbb", "ccc", "ddd");
$sql = "SELECT * FROM list";
$result = mysql_query($sql,$con);
echo mysql_num_rows($result);
?>

I get the following error: "Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\test\mysqli_num_rows.php on line 4"

Can anyone tell me whats wrong with that (simplified) code? Thanks

deotpit
  • 95
  • 1
  • 3
  • 12

2 Answers2

0

Does this work?

<?php
$con = mysql_connect("aaa", "bbb", "ccc", "ddd");
mysql_select_db("database", $con);
$sql = "SELECT * FROM list";
$result = mysql_query($sql,$con);
echo mysql_num_rows($result);
?>
bf2020
  • 742
  • 4
  • 7
  • Hi, It didn't work ! I get the following error: >Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\test\mysqli_num_rows.php on line 5 – deotpit Jul 19 '14 at 04:22
  • I managed to get code that works. It goes like this:> 0) { echo $num; } else { echo 'no record found!'; } ?> Thanks! – deotpit Jul 19 '14 at 06:03
0

I have a hunch that $result is returning false, hence the error about a boolean value. This could mean that either the connection or the query is wrong.

Try putting: $result = mysql_query($sql,$con) or die(mysql_error());

This hopefully would return an error message telling you what is wrong.

Also another point, you probably should be using mysqli or PDO API as mysql is being depreciated.

ImClarky
  • 1,933
  • 1
  • 25
  • 29
  • Hi,This code did work for me ! Left to find what differnce is there between 2 codes. > 0) { echo $num; } else { echo 'no record found!'; } ?> – deotpit Jul 19 '14 at 06:01