2

I just need to get the latest row of a database and echo the id of that row. I can't seem to get it to work. I just get an "Internal Server Error" when I try to load the php page.

<?php 
include ("connection.php");
$query = mysql_query('SELECT id FROM timestamp ORDER BY id DESC LIMIT 1');
        $result = mysqli_query($link, $query);
        $row = mysqli_fetch_array($result);
        echo $row['id'];
?>
kybak
  • 820
  • 3
  • 13
  • 28

5 Answers5

4

Firstly, you're using two different MySQL functions to query with:

$query = mysql_query('SELECT id FROM timestamp ORDER BY id DESC LIMIT 1');
         ^^^^^^^^^^^
        $result = mysqli_query($link, $query);
                  ^^^^^^^^^^^^

and we have no idea what MySQL API you're using to connect with.

Different MySQL APIs do not intermix. You need to use the same one from connection to query.

Remove mysql_query from your code and the brackets are not needed.

$query = 'SELECT id FROM timestamp ORDER BY id DESC LIMIT 1';
        $result = mysqli_query($link, $query);

and assuming a (successful) mysqli_ connection.

Also add or die(mysqli_error($link)) to mysqli_query() should there be any errors in your query.

Additional references:

You can also make use of mysqli_insert_id()

Returns the auto generated id used in the last query


Footnotes:

Anyone asking about timestamp. That isn't a MySQL "reserved" word, but a "keyword". Those are two different animals altogether.

There is no (R) next to it TIMESTAMP, as opposed to say TINYBLOB (R) for example.

Therefore, ticks are not required to be wrapped around their table name.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 1
    If I'm not missing anything, it's also worth mentioning the table named `timestamp`. I know you can name it like that, but you know...^^. _Edit after Footnote_: this comment is more related to a visual than functional subject. It's 'weird' to have a table with that name. – FirstOne Nov 21 '15 at 02:05
  • 1
    @FirstOne I've been questioned before about when an OP was using a keyword & someone said that it needed ticks around it. So, I'm just looking into my crystal ball, once again ;-) and for future visitors to the question. Some think that keywords need to ticked; not entirely true. A keyword would need to be ticked for an INSERT. For example. `INSERT INTO timestamp(col) VALUES('val')`. That's if there is no space between the column name and the brackets. In some systems though, and even with a space `timestamp (col)` than that could throw an error. It can be a bit touchy. – Funk Forty Niner Nov 21 '15 at 02:13
  • 1
    Gotcha. I can't upvote your answer more xD. Anyways, I just thought it was worth mentioning that it might be confusing sometimes and using a more suggestive name for the table would be good too. But you know, maybe that's suggestive enough for the OP. – FirstOne Nov 21 '15 at 02:23
  • @FirstOne Indeed you're right. It's best to try and stay away from using such words; good point ;-) cheers and thanks. – Funk Forty Niner Nov 21 '15 at 02:24
1

Why do you use "mysql"? mysql_ functions are now deprecated. You should use "mysqli" or PDO.mysqli supported same function like "mysql"

Example how to use mysqli

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
VALUES ('Glenn','Quagmire',33)");

// Print auto-generated id
echo "New record has id: " . **mysqli_insert_id($con)**;

mysqli_close($con);
?>

mysqli_insert_id($con) using this you can get last inserted id from database and using this id you can get a row. but if you still need to use mysql then you may use this function mysql_insert_id(); you can get last inserted row id see example below

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');

// it print last inserted id 
printf("Last inserted record has id %d\n", mysql_insert_id());
?>

reffer to this http://php.net/manual/en/function.mysql-insert-id.php

and using that is also fetch last inserted row

toesslab
  • 5,092
  • 8
  • 43
  • 62
Parth Chavda
  • 1,819
  • 1
  • 23
  • 30
  • Did you read the page you're linking to lately? http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – toesslab Nov 21 '15 at 14:01
  • Daenu please read the question they doesn't want to know "mysql" use or "mysqlii"....they want to just know how to get last row...and they use "mysql" not a "mysqli".....we can give suggestion use "mysqli" but according to their question my ans is perfect. – Parth Chavda Nov 23 '15 at 04:57
  • 1
    So why you don't suggest it then? – toesslab Nov 23 '15 at 04:59
  • if i don't give suggestion it doesn't mean my ans is wrong so why "Vote Down" Daenu – Parth Chavda Nov 23 '15 at 05:04
  • I think in the year 2015 it's necessary to point at, at least, so if you mention it in your answer, I'll reverse my downvote – toesslab Nov 23 '15 at 05:10
  • i think Daenu my ans is perfect may it's going vote up now by you...hahah – Parth Chavda Nov 23 '15 at 05:29
0

OOP Style-

<?php 
include ("connection.php");
$query = new mysqli_query('SELECT id FROM timestamp ORDER BY id DESC LIMIT 1');
    $result = new mysqli_query($link, $query);
     $row = new mysqli_fetch_array($result);
    echo $row['id'];
   ?>
fool-dev
  • 7,671
  • 9
  • 40
  • 54
0

try with this code. you have to use "mysqli_query" instead "mysql_query."

<?php 
include ("connection.php"); //contains mysqli_connect information (the $mysqli variable)

$query = mysqli_query('SELECT id FROM timestamp ORDER BY id DESC LIMIT 1');
        $result = mysqli_query($mysqli, $query);
        $row = mysqli_fetch_array($result);
        echo $row['id'];
?>

thanks.

Priya Rajaram
  • 340
  • 2
  • 14
-1

use below query instead

select * from (SELECT id FROM timestamp ORDER BY id DESC) abc limit 1
Maddy
  • 123
  • 4
  • Can you explain lil more – Maduro Nov 21 '15 at 01:49
  • when you put limit clause , it preceeds order by and the result is returned containing only 1 row, and then the ordering happens. In order to get ordering first we need to make a subquery which orders the result and then we get to limit the number of rows. – Maddy Mar 07 '16 at 23:02