1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

need help with showing database rows

Discussion in 'PHP' started by saturn100, Mar 5, 2012.

  1. #1
    Hi
    I am looking to show a row from a table in a data base on a static page website

    I have a script (below) that shows the whole table but i only want to show one row from the table
    I would like to call the row by the field userID

    so in other words call database, table and row userID 1

    any ideas

    <html><head><title>MySQL Table Viewer</title></head><body>
    <?php
    $db_host = 'localhost';
    $db_user = 'root';
    $db_pwd = 'lptm42b';
    
    $database = 'sphinx';
    $table = 'spheres';
    
    if (!mysql_connect($db_host, $db_user, $db_pwd))
        die("Can't connect to database");
    
    if (!mysql_select_db($database))
        die("Can't select database");
    
    // sending query
    $result = mysql_query("SELECT * FROM {$table}");
    if (!$result) {
        die("Query to show fields from table failed");
    }
    
    $fields_num = mysql_num_fields($result);
    
    echo "<h1>Table: {$table}</h1>";
    echo "<table border='1'><tr>";
    // printing table headers
    for($i=0; $i<$fields_num; $i++)
    {
        $field = mysql_fetch_field($result);
        echo "<td>{$field->name}</td>";
    }
    echo "</tr>\n";
    // printing table rows
    while($row = mysql_fetch_row($result))
    {
        echo "<tr>";
    
        // $row is array... foreach( .. ) puts every element
        // of $row to $cell variable
        foreach($row as $cell)
            echo "<td>$cell</td>";
    
        echo "</tr>\n";
    }
    mysql_free_result($result);
    ?>
    </body></html>
    PHP:
    Thanks
     
    saturn100, Mar 5, 2012 IP
  2. amalfra

    amalfra Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try tis :

    append "WHERE userID=Value_you_want" to end of query ..

    Hope tis is what you want ....
     
    amalfra, Mar 5, 2012 IP
  3. bogi

    bogi Well-Known Member

    Messages:
    482
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    140
    #3
    Just change your $result variable:

    $result = mysql_query("SELECT * FROM {$table} WHERE userID = '1'");
    Code (markup):
     
    bogi, Mar 5, 2012 IP
  4. saturn100

    saturn100 Well-Known Member

    Messages:
    465
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    111
    #4
    thanks man
    i was thinging of something like that
    It works
     
    saturn100, Mar 5, 2012 IP