Forum Moderators: coopster

Message Too Old, No Replies

results from a mysql database by newest first

results from a mysql database by newest first

         

scooby545

10:59 pm on Jun 8, 2005 (gmt 0)

10+ Year Member



hi,

I am new to php and mysql and am trying to pull results from my database and display them in date order in a web page.

this is the code i am currently using :

<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("testweb",$db);
$result = mysql_query("SELECT * from testweb2 WHERE date >= DATE()",$db);
echo "$result";
?>

i have been trying to figure things out from tutorials on the web but am really stuck on this as there doesnt seem to be much out there.

i have used the Date() fuction as one of my mysql colums in the format yyyy-mm-dd

can anyone help or tell me the location of a good tutorial?

thanx

Blackie

11:04 pm on Jun 8, 2005 (gmt 0)

10+ Year Member



"SELECT * from testweb2 ORDER BY date ASC" - ascending sort
"SELECT * from testweb2 ORDER BY date DESC" - descending sort

bazzais

11:04 pm on Jun 8, 2005 (gmt 0)

10+ Year Member



Is the format of the column a 'date' type?

HughMungus

11:09 pm on Jun 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use ORDER BY.

SELECT * FROM table ORDER BY datecolumn

or, if you want them in reverse order:

SELECT * FROM table ORDER BY datecolumn DESC

jatar_k

11:10 pm on Jun 8, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld scooby545, (seems everyone else forgot their manners) ;)

well yes we can help you find some good tutorials, try the PHP Library [webmasterworld.com], there are a few threads about mysql and php.

If you still have trouble you can post it. :)

I could also help you with that snippet of code as well.

$result will only contain a resource [ca.php.net], this is like a pointer to the result of your query in mysql. You actually have to use a function to extract each row of data from that resource, like so.

while ($row = mysql_fetch_array($result)) {
echo '<p>';
print_r($row);
}

$row will be an array containing all of the returned columns from your table, I used print_r [ca.php.net] to output them, it is a function for printing variables.

A couple of other small tips.

you don't need to pass the connection to mysql_query, it will use the last opened, unless there is more than one.

You might also want to look at ORDER BY [dev.mysql.com] for your query

>> there doesnt seem to be much out there

good thing you came in here then ;)

scooby545

11:45 pm on Jun 8, 2005 (gmt 0)

10+ Year Member


thanx for the great responses

I have changed the code to :

<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("testweb",$db);
$result = mysql_query("SELECT * FROM testweb2 ORDER BY DATE DESC",$db);
while ($row = mysql_fetch_array($result)) {
echo '<p>';
print_r($row);
}

and am getting an error that says :

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\inetpub\wwwroot\testing\outputform2.php3 on line 52

can you shed some light on how i can fix this?

thanx

bazzais

11:57 pm on Jun 8, 2005 (gmt 0)

10+ Year Member



take the ,$db off the end of your $result = "....

oh and hello - i'm a bit new here too - sorry for any bad etique (or however you spell it!)