Page 1 of 1
I need some simple example ?
Posted: Wed Jan 16, 2008 5:38 am
by omid020
Hi
I`m a beginner and this is my first post here .
I know basics about php and sql .
Now I want to do below actions :
First : I want to catch data from my last row in table of my database and print it on page (suppose that I don`t know ID number of last row in table ).
Second : I have a table that contains a column with "date" name . Now I want to catch data from row that have newest date and print on page .
Please give me some example or give me some tutorial links .
TNX
Re: I need some simple example ?
Posted: Wed Jan 16, 2008 5:52 am
by aceconcepts
In order to get the last row or indeed to sort your records you can use ORDER BY in your sql.
Here is a link to a helpful web site:
http://www.w3schools.com/sql/sql_orderby.asp
To find articles such as this one I would typically use a search engine and enter something like "sql order by"
Hope it helps.
Re: I need some simple example ?
Posted: Wed Jan 16, 2008 6:15 am
by omid020
Thanks aceconcepts ,
But I want a combined example with PHP and Mysql ,
Because I want to show just one row information in both example .
Sorry Google confuse me

! because I`m beginner .
Re: I need some simple example ?
Posted: Wed Jan 16, 2008 6:34 am
by Kieran Huggins
welcome!
try: ORDER BY id DESC LIMIT 1 (assuming you have an id auto increment field in your table, and why wouldn't you?)
Re: I need some simple example ?
Posted: Wed Jan 16, 2008 6:35 am
by aceconcepts
Ok - you don't have to use Google
Anyway, here is an example with both PHP and MySQL:
Code: Select all
//FIRST DEFINE ANY VARIABLES THAT YOU WANT TO USE IN THE QUERY i.e. DATE OR ANY ID YOU MIGHT USE
$date=$_POST['date']; //THIS WOULD SET A VARIABE "$date" EQUAL TO THE VALUE PASSED FROM A FORM "$_POST['date'] - POST is the method used by the form
//NOW SETUP YOU QUERY - a variable is used to store the query string (this is the text used to define the query)
$qry=mysql_query("SELECT * FROM tblName ORDER BY fieldName DESC");
//THE $qry VARIABLE NOW STORES THE EXECUTED QUERY THAT RETRIEVES ALL RECORDS FROM A TABLE IN DESCENDING ORDER
//IN ORDER TO DISPLAY THE RECORDSET/RESULTS YOU WILL NEED TO EXTRACT THE QUERY RESULTS
while($row=mysql_fetch_array($qry))
{
echo $row['field_name1'];
}
//THE ABOVE IS A WHILE LOOP THAT EXTRACT AN ARRAY OF THE RESULTS YOU QUERIED FOR.