hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : Query DB to see if a record exists
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

Query DB to see if a record exists

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 02-07-2006, 10:48 PM
P-nut P-nut is offline
Retired Moderator
 
Join Date: Jun 2003
Location: Proud She-Geek
Posts: 1,719

Query DB to see if a record exists


Oh brother - I can't believe I'm asking this as I know that I know the answer - I just can't seem to figure it out.

I want to query a table in a database (using PHP) to see if a particular thing exists. For this example I'll say I'm looking to see if there is a value of a color in the column color. If the color is in the database, I would like to ouput "This color was already added." If the color is not in the database I would like to output "The color was not found; you should add it now."

So I tried something like this:

PHP Code:
$sql mysql_query("SELECT * FROM `table` WHERE `color` = '".$_GET['color']."'");

if(!
$result) {
echo 
'This color was not found; you should add it now.';
} else {
echo 
'This color was already added';

But I keep getting the message "This color was already added" when I know that it's not there.

Hoping someone will be able to help

__________________
<?php echo "Signature here"; ?>


Last edited by P-nut; 02-07-2006 at 10:52 PM.
Reply With Quote


Sponsored Links
  #2  
Old 02-07-2006, 11:35 PM
BurakUeda BurakUeda is offline
Retarded Noodleator
 
Join Date: Oct 2004
Location: Shimonoseki
Posts: 2,100
that $result should be $sql ?

__________________
Closed for winter...

Reply With Quote
  #3  
Old 02-07-2006, 11:45 PM
P-nut P-nut is offline
Retired Moderator
 
Join Date: Jun 2003
Location: Proud She-Geek
Posts: 1,719
I finally got it to work:

PHP Code:
$result mysql_query("SELECT * FROM `table` WHERE `color` = '".$_GET['color']."'") or die(mysql_error());
   if(
$row mysql_fetch_array($result)) {
echo 
'This color was already added';
   } else {
echo 
'This color was not found; you should add it now.';

Think I need to go to bed

__________________
<?php echo "Signature here"; ?>

Reply With Quote
Sponsored Links
  #4  
Old 02-07-2006, 11:45 PM
MGCJerry MGCJerry is online now
Web Hosting Master
 
Join Date: Jan 2002
Posts: 2,998
use mysql_numrows(), and if the count is 1, its there, and if its 0, its not there...

Something like this. Forgive me if I get the code wrong, I normally use a MySQL class.

PHP Code:
$sql mysql_query("SELECT * FROM `table` WHERE `color` = '".$_GET['color']."'");
if (
mysql_numrows($result)=="1")  {
    echo 
'This color was already added';
} else {
    echo 
'This color was not found; you should add it now.';

Hopefully this points you in the right direction and you get it working.

Reply With Quote
  #5  
Old 02-08-2006, 12:35 AM
null null is offline
Web Hosting Master
 
Join Date: Sep 2002
Location: Illinois
Posts: 2,305
You can also use simpe sql query:

select count(*) from table where color = [color]

It will return zero if color doesn't exist or 1 if it does.


__________________
How's my programming? Call 1-800-DEV-NULL

Reply With Quote
  #6  
Old 02-08-2006, 01:17 AM
hiryuu hiryuu is offline
Web Hosting Master
 
Join Date: Jan 2003
Posts: 1,715
I'm glad you got it working. A couple additional notes:
You should probably end it with 'LIMIT 1', so the server knows it can stop after the first hit. (If color is indexed as unique, then it doesn't matter so much.)

This is just mockup code, but I trust you know that the $_ variables should never go directly into a query. Treat every one of them, even 'trusted' ones like $_ENV, like a live grenade.

__________________
Game Servers are the next hot market!
Slim margins, heavy support, fickle customers, and moronic suppliers!
Start your own today!

Reply With Quote
  #7  
Old 02-08-2006, 01:43 AM
Burhan Burhan is offline
Community Guide
 
Join Date: Jul 2003
Location: Kuwait
Posts: 5,100
In line with what hiryuu said, you should also ALWAYS do some filtering with data that is going to be embedded in a query -- use functions such as mysql_real_escape_string() on any user-submitted data.

I would also recommed you go with null's suggestion because it is a more efficient use of your database, and will probably improve the performance of your script should it ever be put on a heavily-used site. I went into a long discussion on why using COUNT(*) is better than SELECT * in another thread, but I can't remember the topic of it right now.

__________________
In order to understand recursion, one must first understand recursion.
If you feel like it, you can read my blog
Signal > Noise

Reply With Quote
  #8  
Old 02-08-2006, 09:01 AM
P-nut P-nut is offline
Retired Moderator
 
Join Date: Jun 2003
Location: Proud She-Geek
Posts: 1,719
Quote:
Originally Posted by fyrestrtr
In line with what hiryuu said, you should also ALWAYS do some filtering with data that is going to be embedded in a query -- use functions such as mysql_real_escape_string() on any user-submitted data.
Of course Good advice nonetheless

__________________
<?php echo "Signature here"; ?>

Reply With Quote
  #9  
Old 02-08-2006, 09:05 AM
srcnix srcnix is offline
WHT Addict
 
Join Date: Feb 2006
Location: UK, England
Posts: 136
Quote:
Originally Posted by fyrestrtr
I would also recommed you go with null's suggestion because it is a more efficient use of your database, and will probably improve the performance of your script should it ever be put on a heavily-used site. I went into a long discussion on why using COUNT(*) is better than SELECT * in another thread, but I can't remember the topic of it right now.
If you come across the topic again please direct me to it. Sounds interesting.

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Alert Logic Releases New Version of Security and Compliance Solution Web Hosting News 2013-02-19 15:55:16
Google Launches Analytics Tool, Study Shows Firms Favor Cloud Providers for Big Data Needs Web Hosting News 2012-05-02 12:09:31
Web Host Infinitely Virtual Reports Record Growth in 2011 Web Hosting News 2012-01-26 15:59:44
Logicalis Public Cloud Hosting Gets the VMware vCloud Cloud Powered Stamp Web Hosting News 2012-01-04 19:12:16
Web Host 1&1 Launches Domain Registration Mobile App Web Hosting News 2011-11-02 21:05:55


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?