Forum Moderators: coopster
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
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 ;)
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