Determine if a record exists, if so exit PHP/MySQL

I have a table called customers and I need a simple query to check to see if an incoming querystring value is already in that table.  (Field is called "name").

name looks like:  $custName

If the value is found, exit so no more script executes on the page.
JuniorBeeAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Beverley PortlockConnect With a Mentor Commented:
Something like this?

$rs = mysql_query("select * from myTableName where name='$custName' ");
if ( $rs )
    if ( mysql_num_rows($rs) > 0 )
        exit;
0
 
anoyesCommented:
You can call mysql_num_rows on your sql result, and then do exit() if it's greater than 0

<?php
$name = mysql_real_safe_string($_GET['name']);
$rs = mysql_query("SELECT * FROM tablename WHERE name='$name'");
$numRows = mysql_num_rows($rs);
if ($numRows > 0){
  exit();
}
?>

Open in new window

0
 
Beverley PortlockCommented:
Just noticed that you did mention the table name, so amending the above..


$rs = mysql_query("select * from customers where name='$custName' ");
if ( $rs )
    if ( mysql_num_rows($rs) > 0 )
        exit;
0
 
JuniorBeeAuthor Commented:
Trying it out right now, thanks
0
 
JuniorBeeAuthor Commented:
Worked Yay!!!
0
All Courses

From novice to tech pro — start learning today.