SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Dec 2004
    Location
    canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Basic Code to put a mysql table on a PHP page

    Hi there
    I have created a table through my PHPMyadmin center. Now I want to take that table and put it on the web. I was wondering if you could point me to a link/tutorial that will allow me to learn how to "generally" populate a mysql table on the web via php. Or if you could paste the code with some explanation that would be great.

    Thanks

  2. #2
    SitePoint Addict trogdor1024's Avatar
    Join Date
    Oct 2004
    Location
    New Jersey
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?
    // Put stuff into the table
    $link mysql_connect ('db_host''db_user''db_password');
    $db mysql_select_db ('db_name',$link);

    $insert mysql_query ("INSERT INTO tablename (field1, field2)
                             VALUES 'value1', 'value2' "
    );

    mysql_close();
    ?>
    PHP Code:
    <?
    // Retrieve stuff out of the table
    $link mysql_connect ('db_host''db_user''db_password');
    $db mysql_select_db ('db_name',$link);

    $sel mysql_query ("SELECT field1, field2 FROM tablename");
    if (
    mysql_num_rows($sel) > 0) {
     while (
    $fet mysql_fetch_array($sel)) {
       echo 
    '1: ' $fet['field1'] . ', 2: ' $fet['field2'];
       echo 
    '<br />';
     }
    } else echo 
    'No rows returned, nothing in database!';
    mysql_close();
    ?>

  3. #3
    SitePoint Addict trogdor1024's Avatar
    Join Date
    Oct 2004
    Location
    New Jersey
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ooops, forget the explanation part... sorry!

    Example 1 // Put stuff into the table

    1. Connect to the database server using mysql_connect(). Pass the hostname (usually "localhost" or "127.0.0.1") and your database username and password.

    2. Make sure you are talking to the correct database. There can be many databases housed within the db server, just select the database that you used where you created your tables.

    3. Use the mysql_query() function to execute a standard SQL statement. In this instance I am "inserting value1 and value2 as the values of field1 and field2, respectively, into the tablename table".

    4. I am closing the connection to the database with the mysql_close() function.

    ==============================================

    Example 2 // Retrieve stuff out of the table

    Points 1 and 2 are the same, just initiating the connection to the database.

    3. Again, use the mysql_query() function to execute a standard SQL statement. Now I am "selecting field1 and field2 from the tablename table".

    4. If values are returned from the select ("if the number of rows returned is greater than zero"), I want to continue.

    5. Now I fetch the data into a useable array with the mysql_fetch_array() function. This allows me to access the data as...
    $fet['field1'] = value1
    $fet['field2'] = value2
    I throw this in a while loop to cycle through all of the rows and echo it out to the screen in whatever manner I choose.

    6. Again, I close the mysql connection after I'm done with mysql_close().


    --- hope that was a little more in depth.

  4. #4
    SitePoint Addict
    Join Date
    Dec 2004
    Location
    canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot.
    I'll give this a whirl and let you know if I have any questions.

  5. #5
    SitePoint Addict
    Join Date
    Dec 2004
    Location
    canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there
    I tested this right off my desktop and got this on the webpage:

    0) { while ($fet = mysql_fetch_array($sel)) { echo '1: ' . $fet['id'] . ', 2: ' . $fet['description']; echo '
    '; } } else echo 'No rows returned, nothing in database!'; mysql_close(); ?>

    by the way the db name was "winewall" and I replaced "field1" with "id" and "field2" with "description"

    Did I do something wrong?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •