Jump to content


Photo

What did I just do?


  • Please log in to reply
2 replies to this topic

#1 MrLarkins.com

MrLarkins.com
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 13 October 2006 - 11:40 AM

$main = mysql_query("SELECT id FROM ibf_members WHERE mgroup=4");
$main = mysql_fetch_array($main); 
$id = $main['id'];

here is my code.  I know for a fact that there are five entries in the database in column id where mgroup=4, however when i do
$count = count($id);
print("$count");

i get 1

isn't $id an array here?  or did i do something wrong?


#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 13 October 2006 - 11:55 AM

yea you were a little bit off on the syntax..

If all you are wanting to do is count the number of rows you can use ether mysql_num_rows or mysql_result. I will give you an example for both.

The query will still be the same for both..
$main = mysql_query("SELECT id FROM ibf_members WHERE mgroup=4");
$count = mysql_num_rows($main);
echo $count;

Or

$main = mysql_query("SELECT COUNT(id) AS id_count FROM ibf_members WHERE mgroup=4");
$id_count = mysql_result($main, 0, 'id_count');
echo $id_count;

Both will do the same thing

Hope that helps,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 KevinM1

KevinM1
  • Moderators
  • Snarkimus Prime
  • 5,248 posts

Posted 13 October 2006 - 12:11 PM

$main = mysql_query("SELECT id FROM ibf_members WHERE mgroup=4");
$main = mysql_fetch_array($main); 
$id = $main['id'];

here is my code.  I know for a fact that there are five entries in the database in column id where mgroup=4, however when i do
$count = count($id);
print("$count");

i get 1

isn't $id an array here?  or did i do something wrong?


$id isn't an array...$main is an array, with each element being a column in your table.  Right now, you're getting 1 because you've only accessed the id from the one record you've accessed.  Try doing this:

<?php

$query = "SELECT id FROM ibf_members WHERE mgroup=4";
$result = mysql_query($query);
$ids = mysql_num_rows($result); //returns the number of rows returned by your SELECT query

echo "$ids\n";
?>

If you want to get to info from each returned row, you'll have to loop through them as the mysql_fetch functions only return one row at a time.  So something like:

<?php

$query = "SELECT * FROM ibf_members WHERE mgroup=4";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)){ //while you have records available from the SELECT query
   /* process the record, typically with something like:
   $somevar = $row['somecolumn'];
   do something with $somevar */
} //loop automatically ends when there are no more records available from your SELECT query
?>

Using 'global' is a sign of doing it wrong




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users