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.
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).Code:<?php include 'config.php'; $sql = mysql_query('SELECT * FROM `news` ORDER BY `id` DESC LIMIT 0,5'); ?>
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.
Now we have the $row[] array. The $row[] Can be used as followsCode:<? include 'config.php'; $sql = mysql_query('SELECT * FROM `news` ORDER BY `id` DESC LIMIT 0,5'); while($row = mysql_fetch_array($sql)) { } ?>
$row['colname']
For example to Do a full output of the news it Would posibly look like this.
well I hope this shows you how to use mysql_fetch_array.Code:<? include 'config.php'; $sql = mysql_query('SELECT * FROM `news` ORDER BY `id` DESC LIMIT 0,5'); while($row = mysql_fetch_array($sql)) { ?> <?= $row['title'] ?><br> Posted By : <?= $row['poster'] ?> on <?= $row['timestamp'] ?><br> <pre><?= $row'body'] ?></pre> <? } ?>
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
----------
Any Questions E-Mail Affix[@]FedoraProject.org
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks