check if $var is in a mysql table

Posted on 2005-04-16
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

    $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


    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    730 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

    15 Experts available now in Live!

    Get 1:1 Help Now