Jump to content

Recommended Posts

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

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
[code]$count = count($id);
print("$count");[/code]

i get 1

isn't $id an array here?  or did i do something wrong?
Link to comment
https://forums.phpfreaks.com/topic/23838-what-did-i-just-do/
Share on other sites

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..
[code=php:0]
$main = mysql_query("SELECT id FROM ibf_members WHERE mgroup=4");
$count = mysql_num_rows($main);
echo $count;
[/code]

Or

[code=php:0]
$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;
[/code]

Both will do the same thing

Hope that helps,
Tom
Link to comment
https://forums.phpfreaks.com/topic/23838-what-did-i-just-do/#findComment-108269
Share on other sites

[quote author=MrLarkins.com link=topic=111381.msg451313#msg451313 date=1160739628]
[code]$main = mysql_query("SELECT id FROM ibf_members WHERE mgroup=4");
$main = mysql_fetch_array($main);
$id = $main['id'];[/code]

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
[code]$count = count($id);
print("$count");[/code]

i get 1

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

[/quote]

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

[code]
<?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";
?>
[/code]

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:

[code]
<?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
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/23838-what-did-i-just-do/#findComment-108280
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.