Page 1 of 1

[SOLVED]PHp/MySQL Question

Posted: Mon Jun 20, 2005 9:18 am
by JBrown1045
On a form, how can I get the results to be echoed out with the newest being first instead of the oldest first? I went to MySQL.org to see if I could figure out Order By function, but I couldn't get it to work.

Here is what I have:

Code: Select all

CREATE TABLE `tblstory` (
`id` int(11) NOT NULL auto_increment,
`name` text NOT NULL,
`title` text NOT NULL,
`story` text NOT NULL,
`time` int(11) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Stories.php

Code: Select all

<?php
$username="rlzcjewc_danaweb";
$password="dana5899";
$database="rlzcjewc_danastories";

$link = mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die("Unable to select database");

$query = mysql_query("SELECT name,title,story,time FROM tblstory WHERE id>0",$link);
while($row = mysql_fetch_row($query)) {
$dateis = date("m/d/Y", $row[3]);

echo "<b>".$row[1]."</b>";
echo "<br>".$row[2];
echo "<br><br>".$row[0].", ".$dateis;
echo "<br><br><img src=images/break.gif><br><br>";

}//end while.

?>
Insert.php

Code: Select all

<?
$username="rlzcjewc_danaweb";
$password="dana5899";
$database="rlzcjewc_danastories";

mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die("Unable to select database");



if($name==""){
  echo "you didn't enter a name!";
  exit();}
elseif($title==""){
  echo "you didn't enter a title!";
  exit();}
elseif($story==""){
  echo "you didn't enter a story!";
  exit();}
else{
$timePosted = time();
  $query = "insert into tblstory (name,title,story,time) values ('$name','$title','$story','$timePosted')";
  echo "story added! you are being redirected";
}

$result = mysql_query($query) or die(mysql_error());

mysql_close();
?>

<meta http-equiv="refresh" content="1;url=stories.php">

Posted: Mon Jun 20, 2005 9:41 am
by phpScott

Code: Select all

blah ORDER BY col_name DESC
should do the trick

use ASC for ascending and DESC for descending.

Posted: Mon Jun 20, 2005 9:53 am
by JBrown1045
phpScott wrote:

Code: Select all

blah ORDER BY col_name DESC
should do the trick

use ASC for ascending and DESC for descending.
so the line should look like this?

Code: Select all

$query = mysql_query("SELECT name,title,story,time FROM tblstory WHERE ORDER BY col_name DESC>0",$link);

Posted: Mon Jun 20, 2005 11:43 am
by patrikG

Code: Select all

$query = mysql_query("SELECT name,title,story,time FROM tblstory ORDER BY col_name DESC",$link);

Posted: Mon Jun 20, 2005 11:59 am
by JBrown1045
patrikG wrote:

Code: Select all

$query = mysql_query("SELECT name,title,story,time FROM tblstory ORDER BY col_name DESC",$link);
I did that, but its giving me this erro:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/rlzcjewc/public_html/stories.php on line 80

Posted: Mon Jun 20, 2005 12:05 pm
by patrikG
col_name has to be the name of a column in your db-table, e.g.

Code: Select all

$query = mysql_query("SELECT name,title,story,time FROM tblstory ORDER BY time DESC",$link);
The value of "time" determines the order in which the rows are fetched from the db-table. Worthwhile reading up on time & date functions in the MySQL manual.

Posted: Mon Jun 20, 2005 12:24 pm
by JBrown1045
patrikG wrote:col_name has to be the name of a column in your db-table, e.g.

Code: Select all

$query = mysql_query("SELECT name,title,story,time FROM tblstory ORDER BY time DESC",$link);
The value of "time" determines the order in which the rows are fetched from the db-table. Worthwhile reading up on time & date functions in the MySQL manual.
worked perfectly.