Page 1 of 1

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

Posted: Tue Aug 24, 2010 12:04 pm
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?

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

Posted: Tue Aug 24, 2010 1:00 pm
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().