Solved

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

Posted on 2009-03-31
5
286 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
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.

685 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