• Log in or sign up for an account here: https://www.sythe.org/login

[TUT][PHP] Working with Rows

Affix

Newcomer
Joined
Oct 15, 2008
Sythe Gold
0
Messages
12
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
 
Runescape Gamblingrunescape gambling
Back
Top