Searching a database?

Discussion in 'PHP' started by killaklown, Jul 3, 2006.

  1. #1
    How can i make it so that when you type in text in a field and click submit, it searches the database for it and if its found it says "found" and if not it says "not found"

    The code im currently working with is:

    <?
    $sdb = $_POST["check"];
    $query = "SELECT * FROM licensed WHERE domain = $sdb";
    $result = mysql_query($query); 
    
    if($_POST["check"]==""){
    }
    else{
    if($_POST["check"]==$result){
    echo'<br><center>Found</center>';
    }
    else {
    echo'<br><center><font color="red">'.$_POST["check"].' was not found</center>';
    }
    }
    ?>
    Code (markup):
    oh, and i have the config.php and its included on the page i have the above code.:

    
    $db_host = "localhost";
    $db_name = "########";
    $db_user = "##########";
    $db_pass = "#######";
    
    $connection = @mysql_connect ($db_host,$db_user, $db_pass) or die ("Cannot make the connection");
    //connect to database
    $db = @mysql_select_db ($db_name,$connection) or die ("Cannot connect to database");
    
    Code (markup):
     
    killaklown, Jul 3, 2006 IP
  2. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #2
    
    <?php
    $sdb = $_POST['check'];
    $query = "SELECT * FROM licensed WHERE domain = '$sdb'";
    $result = mysql_query($query); 
    $myrecord = mysql_fetch_array($result);
    if($myrecord['domain']!=''){
    echo'<br><center>Found: '.$myrecord['domain'].'</center>';
    }
    else {
    echo'<br><center><font color="red">'.$_POST['check'].' was not found</center>';
    }
    ?>
    
    PHP:
    That should work. There are many ways to pull it off. Notice the single quotes in arrays. You need a loop if you expect more then one record returned from the query.
     
    noppid, Jul 3, 2006 IP
  3. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #3
    Thank you!
     
    killaklown, Jul 3, 2006 IP
  4. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #4
    You're welcome. Did it get ya going in the right direction?

    Are you aware of www.php.net where you can find all the functions? CLick the underlined functions in the php code in my post for a description of that function too.
     
    noppid, Jul 3, 2006 IP