• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

check if $var is in a mysql table

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 .............";
}
0
j_byron
Asked:
j_byron
1 Solution
 
ldbkuttyCommented:
$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.
0
 
j_byronAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now