Grow your CSS skills. Land your dream job.

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

  • # July 25, 2009 at 10:07 am

    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

    # July 25, 2009 at 11:17 am

    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.

    Code:
    < ?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();

    ?>

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".