Forum Moderators: coopster
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?
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?
$sql = "SELECT * FROM users WHERE ID = 1";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].
$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.';
}