treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Find a string in many rows of MySQL table and return a value

  • I want to find a string in many rows of MySQL table and return a value in a different column on the same row.

    Does that make sense? Essentially I want to search for A and return B

    Table1
    ID-------Field1--------Field2
    1--------H-------------G
    2--------N-------------I
    3--------B -------------A
    4--------U-------------W

    I know how to connect and close connections for starters. Just not familiar with querys. Thanks
  • Ok, I solved it after looking harder. I swear google is not my friend when looking up at things like this. This is what I did for anybody elses referance.

    <?php

    $user=\"username\";
    $password=\"password\";
    $database=\"databasename\";
    $host=\"localhost\";

    mysql_connect($host,$user,$password);
    @mysql_select_db($database) or die( \"Unable to select database\");

    $search = $_GET['search'];

    $query = mysql_query(\"SELECT * FROM Table1 WHERE Field2 LIKE '%$search%' LIMIT 0, 30 \");
    while ($row = mysql_fetch_array($query))
    {
    $result=$row[\"Field1\"];
    print $result;
    }

    mysql_close();

    ?>