Solved

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

Posted on 2009-03-31
5
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with mod_substitute 18 77
Change Wording in Wordpress Plugin 4 39
e commerce steps shown instead of self testing 2 56
Sending JSON Via POST In PHP 7 26
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 …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

739 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