Forum Moderators: coopster

Message Too Old, No Replies

DB help!

         

tr8er8

3:38 am on Feb 7, 2008 (gmt 0)

10+ Year Member



I have a site <snip>, and under the "News" section, I want it to pull out all the articles of the db, well only the top 5, and display them there. Now I know I posted something similar to this prior, except it still wont work for some reason.

[edited by: eelixduppy at 4:32 am (utc) on Feb. 7, 2008]
[edit reason] no URLs, please [/edit]

menace_sa

8:05 am on Feb 7, 2008 (gmt 0)

10+ Year Member



First get the top 5 articles, I guess your using mysql and a auto increment id or something.

$gettop5 = "Select TOP 5 from news order by id desc";
$gottop5 = mysql_query($gettop5);

Then, where you want to display it :

<?php
while ($val = mysql_fetch_array($gottop5))
{
$heading = $val["heading"];
$body = $val["body"];

etc...
?>
//YOUR TABLE TO DISPLAY IN HERE
<tr>
<td><?php echo $heading?></td>

etc...
</tr>

<?php }?>

This will loop through the whole resultset displaying one line of data at a time

tr8er8

10:29 pm on Feb 7, 2008 (gmt 0)

10+ Year Member



Do I have to start a new session or type in mysql info or anything?

menace_sa

6:08 am on Feb 8, 2008 (gmt 0)

10+ Year Member



Yes, all the normal connection stuff, no session needed

Ex.

$user = "mysqlusername";
$pass = "mysqlpass";
$host = "mysqlhost";
$database = "mysqldb";

global $host, $user, $pass, $database, $username, $password;
$db_bks = mysql_pconnect($host,$user,$pass);
if (!$db_bks) {
echo "Login failed.";
exit;}

mysql_select_db($database);

tr8er8

6:12 am on Feb 8, 2008 (gmt 0)

10+ Year Member



Yeah I got something goin now thanks.