[TUT][PHP] Working with Rows

Discussion in 'Web Programming' started by Affix, Oct 15, 2008.

[TUT][PHP] Working with Rows
  1. Unread #1 - Oct 15, 2008 at 4:03 AM
  2. Affix
    Joined:
    Oct 15, 2008
    Posts:
    12
    Referrals:
    0
    Sythe Gold:
    0

    Affix Newcomer

    [TUT][PHP] Working with Rows

    Ok in mySQL you have rows in a Table. In these rows you have columns. Each row has a certain number of columns with headers. The easiest way to show this is a News System.

    Each row has its own unique ID

    Each Row has Cols named (example):

    id, title, body, poster, timestamp

    In this tutorial I am going to show you how to use mysql_fetch_array to get each part of these rows.

    Ok First we must create our query. It is important that you put this into a variable.

    PHP:
    include 'config.php';

    $sql mysql_query('SELECT * FROM `news` ORDER BY `id` DESC LIMIT 0,5');

    ?>
    I have Limited the number of rows to fetch this is limited to 5. I have also ordered the Rows by id descending(Highest to Lowest).

    Now we will Want to Fetch our Array. This is a little more difficult than just outputting the result of the query.

    We must use a while() statement.
    PHP:
    include 'config.php';$sql mysql_query('SELECT * FROM `news` ORDER BY `id` DESC LIMIT 0,5');

    while(
    $row mysql_fetch_array($sql)) {

    }

    ?>
    Now we have the $row[] array. The $row[] Can be used as follows

    PHP:
    $row[’colname’]
    For example to Do a full output of the news it Would posibly look like this.

    PHP:
    include 'config.php';$sql mysql_query('SELECT * FROM `news` ORDER BY `id` DESC LIMIT 0,5');

    while(
    $row mysql_fetch_array($sql)) {
    echo 
    $row['article'];

    }
    ?>
    well I hope this shows you how to use mysql_fetch_array.

    This can be applied in more than one way I chose a news system as it is Easier to use. It is used in Member Systems, Forum systems and more..

    hope ive helped you out
     
< [PHP / C] Services | Selling RuneScape forums! >

Users viewing this thread
1 guest


 
 
Adblock breaks this site