简体   繁体   中英

SQL count not working (with mysql_fetch_assoc();)

I am trying to create an admin panel for my website. I use this code on my dashboard to count the number of users (and similar for the blog posts etc.:

//get result
$data = mysql_query("SELECT count(user_id) AS total FROM account_users");
$info = mysql_fetch_assoc($data);

//output result
echo "Rows found :" . $info['total'];

When I enter this code and view the page, this is the result:

Rows found :

As you can see, the number is not visible.

I tried to execute the code in the SQL section of PHPMyAdmin, and the result is 1 (and there is 1 row in the database, so that is correct!)

How is this possible? (And can you help me?)


Update on first comment: I added the code he told me to, but it isn't going better.....

Error I'm getting now:

Notice: Undefined index: auth in /home/vol9_6/epizy.com/epiz_21854614/admin.ictsupport.ga/htdocs/index.php on line 9

Warning: mysqli_query() expects at least 2 parameters, 1 given in /home/vol9_6/epizy.com/epiz_21854614/admin.ictsupport.ga/htdocs/index.php on line 19

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /home/vol9_6/epizy.com/epiz_21854614/admin.ictsupport.ga/htdocs/index.php on line 20
Rows found : 
Notice: Undefined index: username in /home/vol9_6/epizy.com/epiz_21854614/admin.ictsupport.ga/htdocs/nav.php on line 3

mysql_query is deprecated.

Use mysqli_query. Try:

$con=mysqli_connect("localhost","my_user","my_password","my_db");
$sql="SELECT count(user_id) AS total FROM account_users";
$result=mysqli_query($con,$sql);
// Associative array
$row=mysqli_fetch_assoc($result);
echo "Rows found :" . $row['total'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM