Solved

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

Posted on 2009-03-31
5
280 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Trying it out right now, thanks
0
 

Author Comment

by:JuniorBee
Comment Utility
Worked Yay!!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

763 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

9 Experts available now in Live!

Get 1:1 Help Now