View Full Version : make the php code returns true if a mysql record exists...

04-26-2009, 01:33 AM
what i want to do is, make the php code returns true if a mysql record exists in a table, something like:

if(record_exists($record,$table,$db) = true){echo 'yeah! true';}else{echo 'false!';}

how can this be done?

by the way, i'm using php...

04-26-2009, 03:14 AM
Generally speaking, you need to run an SQL statement with specific criteria that targets that record. Something like

WHERE thefield = 'somevalue';

So having that as a base, your 'record_exists' function needs to take some value to check against the database, and return boolean TRUE or FALSE based on the resulting count value, e.g.

function record_exists( $table, $field, $criteria ) {
$s= "SELECT COUNT(*) FROM {$table} WHERE {$field} = '{$criteria}'";
if ( mysql_result( mysql_query($s), 0 ) === 0 ) {
return FALSE;
} else {
return TRUE;

// test
if ( record_exists('usertable','userFirstName','Bob') === TRUE ) {
echo 'Yes, the record exists';

BTW, this

if(record_exists($record,$table,$db) = true)

is using the assigment operator to attempt to make a comparison. You want to use the correct comparison operator (http://us2.php.net/manual/en/language.operators.comparison.php) such as '==' or '===' (which also tests type). Very common programming error, sometimes tough to spot.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum