Having trouble testing if mysql_query() is true or false.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tymlls05
Forum Commoner
Posts: 30
Joined: Tue Nov 01, 2005 1:30 pm

Having trouble testing if mysql_query() is true or false.

Post by tymlls05 »

Code: Select all

	$results=mysql_query('SELECT * FROM `users`;');
	while($row = mysql_fetch_array($results)){
		$test=mysql_query('SELECT `id` FROM `scml_keys` where `id`='.$row['id'].';');
		if(!$test){
			mysql_query('INSERT INTO `scml_keys` (`id`) VALUES ('.$row['id'].');');
			echo 'Adding '.$row['id'].' to keys.<br>';
		}else{
			echo 'Found: '.$row['id'].'<br>';
		}
	}
I have two tables: users and scml_keys.

I am trying to use the script above to see if all of the id's from users exist in scml_keys and if it doesn't, add that id to scml_keys.

For some reason $test is always returning true, even if I empty the table completely. How should I challenge $test to make sure it returns false when an id isn't found?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Having trouble testing if mysql_query() is true or false

Post by AbraCadaver »

$test is true unless there is an error in the query. If you want to see if it returned rows use mysql_num_rows().
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply