I
I
igorc752018-01-29 10:53:12
PHP
igorc75, 2018-01-29 10:53:12

Submitting a form via javascript, how to get the id of the last post?

if you just pass echo "1" - then in alert (data) - shows 1, and if

$query = "SELECT id FROM `post` ORDER BY id DESC Limit 0,1";
$result = mysql_query($query) or die(mysql_error());
echo $result['id'];

it's empty. (there are records in the database, I tried it in SQL - it returns, for example, id: 141)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
igorc75, 2018-01-29
@igorc75

Thanks for the correction, this is how I solved it.

$query = "SELECT `id` FROM `post` ORDER BY `id` DESC LIMIT 1";
$result = mysql_query($query) or die(mysql_error());
$array = mysql_fetch_assoc($result);
echo $array['id'];

O
OKyJIucT, 2018-01-29
@OKyJIucT

The mysql_query function returns a resource, not a query result. See example #2 php.net/manual/en/function.mysql-query.php

T
TheRevan, 2018-01-29
@TheRevan

"SELECT `id` FROM `post` ORDER BY `id` DESC LIMIT 1"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question