Solved

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

Posted on 2009-03-31
5
281 Views
Last Modified: 2012-05-06
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.
0
Comment
Question by:JuniorBee
  • 2
  • 2
5 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 24033377
Something like this?

$rs = mysql_query("select * from myTableName where name='$custName' ");
if ( $rs )
    if ( mysql_num_rows($rs) > 0 )
        exit;
0
 
LVL 16

Expert Comment

by:anoyes
ID: 24033391
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
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24033394
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
 

Author Comment

by:JuniorBee
ID: 24033398
Trying it out right now, thanks
0
 

Author Comment

by:JuniorBee
ID: 24033895
Worked Yay!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now