Using PHP to query a MySQL Database table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samui
    New Member
    • Sep 2006
    • 8

    Using PHP to query a MySQL Database table

    I'm trying to get a php page to show data from a MySQL database. But the problem that I have is I don't know where to start.

    I'm very new at PHP and I'm constantly told by others that it's very hard to just jump straight into it, but that's the best way I learn.

    All I need to know is how to show all the information I have on a MySQL database table in a table on a web page. From there I should be able to create a form that I can select certain pieces of data to be shown.

    If anyone could offer their expertise in this, it would be very appreciative. Thank you.
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    The following sample reads 3 columns of data (name, address, city) from the MySql database table table_name and displays that on the screen in a HTML table.
    [PHP]<?php
    $conn = mysql_connect(" localhost", "username", "password")
    or die($msg_no_con nect);
    mysql_select_db ("vwso")
    or die(mysql_error ());
    $res = mysql_query("SE LECT name, address, city FROM table_name;")
    or die(mysql_error ());
    if (mysql_num_rows ($res) > 0) {
    echo '<table border="1">';
    echo '<th>Name</th><th>Address</th><th>City</th>';
    while ($row = mysql_fetch_ass oc($res)) {
    echo "<tr>
    <td>{$row['name']}</td>
    <td>{$row['address']}</td>
    <td>{$row['city']}</td>
    </tr>";
    }
    echo '</table>';
    }
    else
    echo 'No rows in selected table';
    ?>[/PHP]

    Ronald :cool:

    Comment

    • samui
      New Member
      • Sep 2006
      • 8

      #3
      Awesome thank you so much, that gave me a huge grip on things. Very helpful indeed! =D

      Comment

      Working...