Forum Moderators: coopster

Message Too Old, No Replies

MySQL Array

         

NancyJ

9:34 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



I was hoping someone could answer this or point me in the direction of a tutorial about the output of mysql_fetch_array
now I'm totally fine using it and dealing with it when I have only 1 record that matches the SQL query ie.

SELECT * FROM users WHERE ID = 1

I know from that I will get an array with all the fields from the database for that record and I can process the output like

$data = mysql_fetch_array($sql);

and then if I want to retrieve the value of a field then I can

$data['field']

but what if there are multiple entries that match the criteria - is it as simple as a multidimensional array?

I mean could I loop through the results and output them just by saying

while($count < = mysql_num_rows($sql)){
echo $data['field][$count];
}

Is that right?

NancyJ

9:49 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



So I tried that and it output 1 character per line of 1 entry in the database.... not quite what I had in mind...

NancyJ

9:59 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



I guess it would help if I explained what I was trying to do.
I have a db with news updates for my website. Instead of manually archiving them every time I add a new update /yawn I want to display only the 10 most recent news items on the front page then generate links to the archived news items at the bottom of the page.

so this is what I've got so far:


if ($page = main){
include 'db.php';
$News = $HTTP_GET_VARS['news'];
$PerPage = 10;

$sql = mysql_query("SELECT * FROM News ORDER BY Date DESC") or die (mysql_error());
$NewsItems = mysql_num_rows($sql);
$Pages = Round($NewsItems/$PerPage, 0);

if (($Pages * $PerPage) < $NewsItems) { $Pages = $Pages +1; }

$Count = ($News - 1) * $PerPage;

if (($News * $PerPage) > $NewsItems) {$Max = $NewsItems;}
else { $Max = ($News * $PerPage);}

while($row = mysql_fetch_array($sql)){

while ($Count <= $Max) {

$Date = $row["Date"];
$NewsItem = $row["NewsItem"];

$Date = strtotime($Date);

$Date = date("l jS of F Y", $Date);
echo "<img src = \"images/icon.jpg\"> <b>$Date</b><hr>$NewsItem";

$Count = $Count +1;
}
}
}

Except it prints out the same news item 10 times...
Where am I going wrong?

coopster

12:21 pm on Dec 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Think of the result set returned as a "spreadsheet" of data, columns and rows. You need to loop down through the rows, fetching each row as you go. Once you fetch the row, you can pull information from the columns. You'll often see while [php.net] control structures used for processing result sets.
$sql = "SELECT * FROM users WHERE ID = 1"; 
$result = mysql_query($sql);
if (mysql_num_rows($result) > 0) {
while($data = mysql_fetch_array($result)){
print $data['field'] . "<br />\n";
}
} else {
print 'Nothing found.';
}
There is a short and simple tutorial in our own PHP Library [webmasterworld.com] titled Basics of extracting data from MySQL using PHP [webmasterworld.com].