Jump to content

Recommended Posts

Hi guys,

 

I've run into a seemingly strange problem.  I am coding a very simple search function looking in just one mysql table, but whenever I use the query "SELECT * FROM `table` WHERE `field`='value'"  I only get the last record in the table the WHERE statement applies, as opposed to all of them.  Does anyone know what could be causing this?

 

Simplified Code:

$query = mysql_query( "SELECT * FROM users WHERE `Country`='Australia'" );
print mysql_num_rows($query) . '<br />';
while ( $person = mysql_fetch_object( $query ) ) {
	print $person->id;
}

 

I consistently only get one record, although there are quite a few more than 1 in the db.

 

Link to comment
https://forums.phpfreaks.com/topic/185713-mysql-select-problem/
Share on other sites

I'm not sure but try using mysql_fetch_assoc() instead of mysql_fetch_object() as well as $person['id'] rather than $person->id.

 

I can't think of any other reason in all honesty. :-\

 

EDIT: I've just seen you haven't declared the connection when querying. I believe you need to use:

 

$query = mysql_query("SELECT * FROM users WHERE `Country`='Australia'", $connection);

Link to comment
https://forums.phpfreaks.com/topic/185713-mysql-select-problem/#findComment-980685
Share on other sites

I'm not sure but try using mysql_fetch_assoc() instead of mysql_fetch_object() as well as $person['id'] rather than $person->id.

 

I can't think of any other reason in all honesty. :-\

 

EDIT: I've just seen you haven't declared the connection when querying. I believe you need to use:

 

$query = mysql_query("SELECT * FROM users WHERE `Country`='Australia'", $connection);

 

I know how to use both mysql_fetch_assoc and mysql_fetch_object, and regardless they should both work.  The query is pulling from the right database (there's only one anyway) so the connection isn't the issue.  Ugh.  Thanks for your help though.

Link to comment
https://forums.phpfreaks.com/topic/185713-mysql-select-problem/#findComment-980691
Share on other sites

figured it out.  Damn whitespace.

Do you mean the whitespace in mysql_query()? If so, I removed that above as it was annoying me. I should have mentioned it but like you, I honestly didn't think it would make any difference. It was kind of at the back of my mind but I didn't want to mention it in case it was a stupid suggestion. :P Glad you got it sorted anyway.

Link to comment
https://forums.phpfreaks.com/topic/185713-mysql-select-problem/#findComment-981073
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.