PDA

View Full Version : Reading from MySQL(PHP)



Liquid-Fire
18 Dec 2005, 12:57 PM
Ok I am a little new to php and what to use mysql for my news on my website. Now this is the order my my mysql table for the new:

id
date
auther
title
summary
fullstory

now this is the code for displaying a piece of news



<h2>November 22nd</h2>
<h3><?php echo( $row ); ?></h3>
<p>this is just a bunch text for displaying purpose</p>


Now I need the date to been in between the <h2>, and the title in between <h3> and the summary in between <p> becuase I am using a .css file to display the news in a certian way. Now this is how I am connecting to me db and is is working fine.


<?php
$database = "kaizendigital";
$dbloginname = "root";
$dbpassword = "";
$host = "localhost";

$link = mysql_connect( $host, $dbloginname, $dbpassword ) or die( 'could not connect' . mysql_error() . '.') ;
$selecteddb = mysql_select_db( $database ) or die( 'could not connect to ' . $database . ' please check to make sure it exists.' );

$query = 'SELECT * FROM `news`';
$result = mysql_query( $query ) or die('could not perform ' . $query . ' query.' );
?>

I need there are a few thing I need some help on. First how can I extract certain data for the db into a php variable like how cna i make


$title = title in the mysql db

The second thing I need help on is how only run the process for only to 30 newest news. I was thinging of doing something like:


for( $i = 0; $i < 30; $i++ )
{
//php code to print out news
}

but I dont know how to prin the newest news becuase I was thinking of id but the id goes forward.
The third thing i need help on is how do I know which news item is the newest and how do i select that. Any help would be great, thanks.

sm9ai
20 Dec 2005, 10:43 AM
Okay. A very simple way of doing it(there are other methods)

$query = 'SELECT * FROM `news` ORDER BY date DESC LIMIT 0,30';
$result = mysql_query( $query ) or die('could not perform ' . $query . ' query.' );
while ($row = mysql_fetch_row($result))
{
$title=$row[3];
etc.
}

That should do it. You will need to put the display code within the brackets as well.

Notice the changes to the SQL statement. ORDER BY orders the data and LIMIT restricts the results to 30 starting from row 0. e.g. the dirst 30 records.