nisroc Posted December 30, 2013 Share Posted December 30, 2013 (edited) Still trying to get started with php/mysql. My new problem is i wish to count the number of rows in my table so that i can preform a loop when counting multiple column entries. example i have the number 23 listed in 6 different columns and i wish to find out how many times 23 is listed. So i am trying to get a row count which is 3096 and then create a script. problem is the script below i count the rows using SELECT COUNT(*) but the return comes out as the quote below the script. I am obviouly doing something wrong but cannot seems to find a different way to get just 3096 as a return. any help on this... please and thanks. $con = mysql_connect($hostname, $username, $password, $database) or die("Unable to connect to MySQL"); mysql_select_db($database); $result = mysql_query("SELECT COUNT(*) FROM Draws"); $row = mysql_fetch_assoc($result); echo '<pre>'.print_r($row,true).'</pre>' Array([COUNT(*)] => 3069) Edited December 30, 2013 by nisroc Quote Link to comment https://forums.phpfreaks.com/topic/284982-weird-return/ Share on other sites More sharing options...
mac_gyver Posted December 30, 2013 Share Posted December 30, 2013 @nisroc, this forum section - PHP Regex is not for general php questions. this is the third recent post you have made in this section. you will get more, faster, better replies to your php questions in the PHP Coding Help forum. moving this thread for you... Quote Link to comment https://forums.phpfreaks.com/topic/284982-weird-return/#findComment-1463312 Share on other sites More sharing options...
dalecosp Posted December 30, 2013 Share Posted December 30, 2013 (edited) When you call "mysql_fetch_assoc", you're asking for an associative array, which is what you're getting. This would work with the old mysql_ functions you're using: $result = mysql_result(mysql_query("SELECT COUNT(*) FROM Draws")); You really *should* be using mysqli_ instead: $con = mysqli_connect($hostname, $username, $password, $database) or die("Unable to connect to MySQL"); $result = mysqli_query($con,"SELECT COUNT(*) FROM Draws"); $num_rows = $result->num_rows; echo "There are $num_rows results from your query."; Edited December 30, 2013 by dalecosp Quote Link to comment https://forums.phpfreaks.com/topic/284982-weird-return/#findComment-1463313 Share on other sites More sharing options...
Barand Posted December 30, 2013 Share Posted December 30, 2013 $result->num_rows will always be 1 with that query! try $result = mysqli_query($con,"SELECT COUNT(*) FROM Draws"); list($num_rows) = $result->fetch_row(); echo "There are $num_rows results from your query."; Quote Link to comment https://forums.phpfreaks.com/topic/284982-weird-return/#findComment-1463318 Share on other sites More sharing options...
dalecosp Posted December 30, 2013 Share Posted December 30, 2013 D'oh, too true! I need to ask the boss for more coal in the stove ... brain's apparently frozen. That would be the reason I have this: function mysqli_result($result,$row,$field) { if (@$result->num_rows==0) return 'unknown'; $result->data_seek($row); $one=$result->fetch_assoc(); $ret=$one[$field]; return trim($ret); } Quote Link to comment https://forums.phpfreaks.com/topic/284982-weird-return/#findComment-1463327 Share on other sites More sharing options...
nisroc Posted December 30, 2013 Author Share Posted December 30, 2013 @nisroc, this forum section - PHP Regex is not for general php questions. this is the third recent post you have made in this section. you will get more, faster, better replies to your php questions in the PHP Coding Help forum. moving this thread for you... \ Thank you and sorry for posting in wrong section Quote Link to comment https://forums.phpfreaks.com/topic/284982-weird-return/#findComment-1463331 Share on other sites More sharing options...
nisroc Posted December 31, 2013 Author Share Posted December 31, 2013 $result->num_rows will always be 1 with that query! try $result = mysqli_query($con,"SELECT COUNT(*) FROM Draws"); list($num_rows) = $result->fetch_row(); echo "There are $num_rows results from your query."; this worked but "->" got me confused. Forgive me for probably asking the stupidest question but googling it did not work. what is "->" Quote Link to comment https://forums.phpfreaks.com/topic/284982-weird-return/#findComment-1463471 Share on other sites More sharing options...
kicken Posted December 31, 2013 Share Posted December 31, 2013 this worked but "->" got me confused. Forgive me for probably asking the stupidest question but googling it did not work. what is "->" Have a read through this page: Class Properties. Quote Link to comment https://forums.phpfreaks.com/topic/284982-weird-return/#findComment-1463472 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.