Jump to content

Query does not work in script, yet does when manually entered in PHPMyAdmin


TexasMd91

Recommended Posts

Server version: 5.1.53-log

 

I have the following queries

SELECT user_id FROM phpbb_profile_fields_data WHERE pf_rsname = "Atroxide" LIMIT 1 
SELECT user_id FROM phpbb_profile_fields_data WHERE pf_rsname = "Delia Smith" LIMIT 1 
SELECT user_id FROM phpbb_profile_fields_data WHERE pf_rsname = "espinozagabe" LIMIT 1 
SELECT user_id FROM phpbb_profile_fields_data WHERE pf_rsname = "Jaunty1" LIMIT 1 
SELECT user_id FROM phpbb_profile_fields_data WHERE pf_rsname = "lvoos" LIMIT 1 

All 5 of these queries are executed at a different time (in a foreach loop).

All 5 except for the one below returned a result.

SELECT user_id FROM phpbb_profile_fields_data WHERE pf_rsname = "Delia Smith" LIMIT 1 

I couldn't figure out why it wasn't working so I copy pasted it into PHPMyAdmin and it returned the result I was looking for.

 

What could cause for PHPMyAdmin to work but not the exact same query in a php script to not? It didn't return an error using mysql_error() either.

 

Pretty sure its irreverent but here is the php script.

			foreach ($online as $username => $activity) {
			$query = "
				SELECT user_id
				FROM phpbb_profile_fields_data
				WHERE pf_rsname = \"" . $username . "\"
				LIMIT 1
			";
			$result = $db->query($query);
		}

 

The table is

user_id	        mediumint(		UNSIGNED	No	0		 	 	 	 	 	 	
pf_rsname	varchar(255)	utf8_bin		Yes	NULL		 	 	 	 	 	 	 

 

 

 

 

Don't run a query in a loop when it isn't absolutely necessary.

 

$online_now = implode( "', '", $online );
$query = "SELECT user_id FROM php_bb_fields_date WHERE pf_rname IN( '$online_now' )";

You likely have a non-printing character as part of the username and it causes a mis-match with the data in the table.

 

I recommend using var_dump() on the value in the variable so that you can see how many characters it contains.

 

Where are these values coming from?

Don't run a query in a loop when it isn't absolutely necessary.

 

$online_now = implode( "', '", $online );
$query = "SELECT user_id FROM php_bb_fields_date WHERE pf_rname IN( '$online_now' )";

Thanks but I don't think that would work with the rest of my script.

I need to associate user_id's with the usernames and activities within an array and insert those three things, and a timestamp into another table. Is there still a way to pair up these results with their respected array(username, activity)?

 

and also, not sure if that was supposed to fix the problem i was having, but still having the issues.

 

I am pretty much certain it has to do with spaces, what does phpmyadmin do to spaces that any query in php wouldn't do? Ive done a few google searches and still can't get a good query on google to get results.

 

and to answer PFMaBiSmAd's question, here is the result of doing the var dump on the array

array(5) {
  ["Delia Smith"]=>
  string(12) "A string"
  ["espinozagabe"]=>
  string(13) "A string"
  ["Jaunty1"]=>
  string(12) "A string"
  ["Xtr3m3Legend"]=>
  string(13) "A string"
  ["xxx jester x"]=>
  string(12) "A string"
}

(different names because different people are online now, but its definitely dealing with the space because "xxx jester x"'s query doesn't work either.)

EDIT: I also edited the actual value of the array, can't show em here.

 

I am getting these values from a XML file.

Can't seem to find an edit button, guess its removed when the post is a certain amount of time old?

 

I have reorganized how the array is to so that I tested the var_dump() and its 11 characters, which "Delia Smith" is.

Figured out what was doing, the spaces were not true spaces, if you entered them into php, it would output a true space in the browser, which is why when i printed the query and typed the query into PHPMyAdmin it would work.

 

to solve I used preg_replace to remove all characters that weren't [a-zA-Z0-9 ] and replace with a space.

 

$username = preg_replace("[\W]"," ",$username

 

Thanks all those who helped :)

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.