|
|
 |
RE: FN-FORUM: PHP/MySQL Question...
date posted 8th February 2008 10:10
Thanks Nick...Pamela & Anthony.... Sorted now...
Darren
-----Original Message-----
From: [EMAIL REMOVED] [EMAIL REMOVED] On Behalf Of =
Anthony Cartmell
Sent: 07 February 2008 22:33
To: FN-FORUM / [EMAIL REMOVED]
Subject: Re: FN-FORUM: PHP/MySQL Question...
> $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 =20
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, =20
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, =
which =20
is more readable and more robust. (There's also mysql_fetch_object which =
=20
allows for the slightly neater $row->NewsHeadline etc. but doesn't work =
so =20
well if you have "-" characters in your field names.)
HTH,
Anthony
--=20
www.fonant.com - Quality web sites
--=20
Freelancers, contractors earn more with Prosperity4
Call 0870 870 4414 or visit www.prosperity4.com
and benefit from Inland Revenue approved expenses today.
To advertise here: http://www.freelancers.net/advertising.html
|
 |
|