|
|
 |
Re: FN-FORUM: PHP/MySQL Question...
date posted 7th February 2008 21:19
> $rows_per_page =3D 4;
> $db =3D 'NBPA';
> $sql =3D "SELECT * FROM News";
> $result =3D mysql_query($sql);
> $total_records =3D mysql_num_rows($result);
Might be more efficient to get MySQL to do the count (it probably doesn'=
t =
even need to look at the data):
$sql =3D "SELECT COUNT(id) FROM News";
$result =3D mysql_query($sql);
$total_records =3D mysql_result($result,0);
> $sql =3D "SELECT * FROM News ORDER BY NewsDate ";
> $sql .=3D "LIMIT $start, $rows_per_page";
> $result =3D mysql_query($sql);
> $rows =3D mysql_num_rows($result);
> for ($i =3D 0; $i < $rows; $i++)
> {
> $description =3D mysql_result($result, $i, 0);
>
> echo "\$description =3D $description\n";
> }
You're only getting column 0 from the result row, you probably mean one =
of
mysql_result($result,$i,1);
mysql_result($result,$i,'NewsHeadline');
I'd write that to be more efficient by getting each row as an array, =
something like:
while ($row =3D mysql_fetch_assoc($result)) {
echo 'Headline =3D '.htmlspecialchars($row['NewsHeadline'])."\n"=
;
echo 'Text =3D '.htmlspecialchars($row['NewsText'])."\n";
}
using mysql_fetch_assoc() means you can refer to the fields by name, whi=
ch =
is more readable and more robust. (There's also mysql_fetch_object which=
=
allows for the slightly neater $row->NewsHeadline etc. but doesn't work =
so =
well if you have "-" characters in your field names.)
HTH,
Anthony
-- =
www.fonant.com - Quality web sites
|
 |
|