mySQL Rows in PHP

Status
Not open for further replies.

Baljeet

Member
Jan 31, 2011
76
0
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.

Code:
<?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.

Code:
<?
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

$row['colname']

For example to Do a full output of the news it Would posibly look like this.

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>

<?
}
?>

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

----------

Any Questions E-Mail Affix[@]FedoraProject.org

All Credits goes to one who really made this...
 
Status
Not open for further replies.

Users who are viewing this thread

Top