Jump to content

Recommended Posts

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 by nisroc
Link to comment
https://forums.phpfreaks.com/topic/284982-weird-return/
Share on other sites

@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...

Link to comment
https://forums.phpfreaks.com/topic/284982-weird-return/#findComment-1463312
Share on other sites

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 by dalecosp
Link to comment
https://forums.phpfreaks.com/topic/284982-weird-return/#findComment-1463313
Share on other sites

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);
}

Link to comment
https://forums.phpfreaks.com/topic/284982-weird-return/#findComment-1463327
Share on other sites

@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

Link to comment
https://forums.phpfreaks.com/topic/284982-weird-return/#findComment-1463331
Share on other sites

 

$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 "->"

Link to comment
https://forums.phpfreaks.com/topic/284982-weird-return/#findComment-1463471
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.