How to get the last field in a Mysql database with PHP? - php

I have mysql database and I want to get the last ID from field in a table .
example : -
id Value
1 david
2 jone
3 chris
I want a function return 3 if this table exist .

You can use:
SELECT MAX(id) FROM table

If you want to select the ID of the most recently inserted row in a table with an AUTO_INCREMENT column, you will likey be interested in MySQL's LAST_INSERT_ID function.

SELECT id
FROM table
ORDER BY id DESC
LIMIT 1

you can also use the following code ::
$lastId = mysql_insert_id();

SELECT * FROM table ORDER BY id DESC LIMIT 1

Related

how to insert data in max id by using sql query

I want to get the maximum id of row data. In my table first column is id then firstname. this is the sql command I used to get the max(id) of row data and insert firstname in max(id) row. but it is not working
<?PHP $maxid=mysql_query("insert into test(firstname) values ('$sym') where id =max('id') ;?>
You can achieve your requirement in this way.
UPDATE test
SET firstname = '$sym'
ORDER BY `id` DESC LIMIT 1;
UPDATE yourtable
SET firstname = '$sym'
WHERE id in (SELECT max(id) from yourtable)

How can I get the last row from a table mysqli?

I have a table and am trying to get the most recent row using this code:
include "db_conx.php";
$sql="SELECT column FROM table ORDER BY DESC LIMIT 1";
if ($result=mysqli_query($db_conx,$sql))
{
while ($row=mysqli_fetch_row($result))
{
printf($row[0]);
}
mysqli_free_result($result);
}
It returns a blank result though.
ORDER BY DESC LIMIT 1
ORDER BY what DESC? You have to provide a column name where which you want to order by. Could be an Auto increment column, primary key or even the timestamp etc
As it currently stands your query has invalid syntax and will not return anything other than an error.
For the ORDER BY you should give a column name of the table.
Ex : SELECT * FROM TABLE_NAME ORDER BY table_id DESC LIMIT 1
In your query you missed the column name to order by.
you missed column name after order by clause. hope it helps !

How can i get the last record from database by php

Is there any way to get the last record from database without using:
SELECT * FROM 'myTabe' ORDER BY id DESC LIMIT 1
This code selects LAST INSERTED ROW:
SELECT * FROM 'mytable' WHERE id = LAST_INSERT_ID();

mysql WHERE clause not working

I would like to select multiple values in where clause but it is not selecting anything.
This is the select query I have:
'SELECT * FROM table WHERE id IN (4, 5) ORDER BY id desc'
what am I missing?
Based on your comments, the reason that the query is failing is because the column is a varchar and you are using int values in your IN clause. MySQL does not convert the type if you quote the numbers then your query will work with varchar
http://dev.mysql.com/doc/refman/5.0/en/type-conversion.html
I would imagine your table has no data with id = 4 or id = 5.
Try
SELECT * FROM table WHERE id = 4
Does that return anything either? I would bet no.
Why not just 'SELECT * FROM table WHERE (id = '4' OR id= '5') ORDER BY id desc'

Select a row where one column is min

I'm writing a simple URL rotator for a client and they want the URLs to rotate according to the oldest one that was previously displayed.
My columns are very simple:
url_id | company_id | url | last_clicked
I want to fetch a single row where the company_id is passed in and the last_clicked is the minimum of all records matching the company_id.
It should also select a random url_id if all last_clicked values are empty.
I assume this can be accomplished with a GROUP BY and HAVING but I can't seem to get the query to return anything.
I have this:
$last = $this->db->fetchOne ("SELECT url_id FROM
{$this->prefix}urls GROUP BY company_id HAVING MIN(last_clicked)
WHERE company_id='$company'");
This will fetch the row where last_clicked is smallest, or at random if they are all NULL:
SELECT url_id FROM urls
WHERE company_id = $company
ORDER BY last_clicked, RANDOM() LIMIT 1;
An index based on company_id and last_clicked would greatly help:
CREATE INDEX urls_ndx ON urls(company_id, last_clicked);
SELECT url_id FROM
TABLE
WHERE company_id='$company'
AND last_clicked = ( SELECT MIN(last_clicked)
FROM TABLE WHERE company_id='$company' )

Categories