0

I want the last value from my query . i tried end(); did not work..

$last_ad_idx=mysql_query("SELECT ads_id FROM ads WHERE id='$id'");
$target_ad_id = mysql_fetch_array($last_ad_idx);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • There's only one column in the array. Just use `$target_ad_id['ads_id']`. – Barmar Oct 14 '16 at 19:34
  • RTFM: [fetch_array()](http://php.net/mysql_fetch_array): `Fetch a result row` - **ROW** -- not row(S). – Marc B Oct 14 '16 at 19:34
  • Try adding a limit: SELECT ads_id FROM ads WHERE id='$id' LIMIT 1 – john Oct 14 '16 at 19:34
  • If you mean the last row, that doesn't really make sense, since you didn't specify any ordering of the results. – Barmar Oct 14 '16 at 19:35
  • Use `SELECT MAX(ads_id) AS last_ads_id FROM ads WHERE id='$id'` – Barmar Oct 14 '16 at 19:36
  • the ads_id column is a primary key auto increment.. so the id value is the highest. that's what i need. (the last highest value in that column) –  Oct 14 '16 at 19:42
  • IT worked bro.. –  Oct 14 '16 at 19:45
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Oct 14 '16 at 19:56
  • ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Oct 14 '16 at 19:56

1 Answers1

2
 SELECT ads_id FROM ads WHERE id='$id' order by ads_id desc limit 1
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119