[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


check if $var is in a mysql table

Posted on 2005-04-16
Medium Priority
Last Modified: 2013-12-12
If I have a PHP variable and want to check if it is in a row of a mysql table, how do I do this?

e.g. if ($variable '''is in table''' && some other constraints)
"INSERT .............";
Question by:j_byron
LVL 32

Accepted Solution

ldbkutty earned 1000 total points
ID: 13796976
$result = mysql_query("SELECT COUNT(*) FROM table_name WHERE column_name = '" . mysql_real_escape_string($variable) . "'") or die("Sql error: " . mysql_error());
$row = mysql_fetch_array($result);
if($row[0] > 0 && some_other_constraints)
 // INSERT ....

// Use the LIKE operator search if the column has value in addition to $variable.

Author Comment

ID: 13797009

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
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…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month19 days, 15 hours left to enroll

872 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