-3

I'm trying to get a count of how many entries are in a database by using COUNT in SQL similar to this. This is my code, it returns/echos nothing.

$result=mysqli_query($con,"SELECT COUNT(id) as total FROM Customer");

if (!$result) echo mysql_error();

$row=mysqli_fetch_assoc($result);

echo $row['total'];

$id=$row['total'];

echo($id);

What is wrong? The connection info is correct and has no errors.

Update: I still need help with this. Please stop downvoting and not giving answers. If it's a question with an obvious answer please tell me.

Update: This is not at all a duplicate of that question. The issue was not specifically with error reporting.

Update: I answered this question and figured out my problem was with the default database which is why nothing was being returned. I can't select my answer until tomorrow.

Community
  • 1
  • 1
Eddie Hartman
  • 58
  • 1
  • 10
  • Try echo ```"

    Row

    ".print_r($row,true)."
    "``` to see the result set. That may clear up how to reference what is returned.
    – Sloan Thrasher Apr 16 '17 at 21:03
  • That just printed "Row " there was nothing after. – Eddie Hartman Apr 16 '17 at 21:20
  • Try changing ```if (!$result) echo mysql_error();``` to ```echo mysql_error();```. If the echo in my first comment doesn't show anything, there's a problem getting the row. – Sloan Thrasher Apr 16 '17 at 21:27
  • Just for grins and giggles, also put the ```echo mysql_error();``` after the fetch. – Sloan Thrasher Apr 16 '17 at 21:28
  • There is no error. I can do that query in phpmyadmin and I get a number (what I want) ex. 10. I put it in both places and got no errors. – Eddie Hartman Apr 16 '17 at 22:53
  • I think the downvotes are because you are using mysqli embedded. Try using stored procs – Malcolm Salvador Apr 16 '17 at 23:20
  • I'm doing this for a class and this is how we were taught. What do you mean stored procs? – Eddie Hartman Apr 17 '17 at 00:06
  • If the echo .. row didn't print anything other than Row, then the query didn't return anything, and there is a problem with the query. The SQL looks fine, but there's a problem elsewhere in the php code. For instance, you are mixing mysqli and mysql functions. – Sloan Thrasher Apr 17 '17 at 01:50
  • Yeah I knew that. So I made a simple mistake. I never put mysqli_select_db ( $con, "....."); Into my file so it never knew what database to use. I also was not getting errors because I was using mysql_error instead of mysqli_error. Thank you for the help. – Eddie Hartman Apr 17 '17 at 03:02

3 Answers3

1

You are using MySQLi AND calling mysql_error(). Should be

mysqli_error($con);

Now you should see error messages, if there are any.

David Vareka
  • 252
  • 2
  • 6
0

I never put mysqli_select_db ( $con, "....."); Into my file so it never knew what database to use. I also was not getting errors because I was using mysql_error instead of mysqli_error. Once I selected the default database, it worked fine.

Eddie Hartman
  • 58
  • 1
  • 10
0

echo out mysqli_error($conn) instead of mysql_error()