-2

I am having an issue declaring a PHP variable as a MySQL result.

Code:

$num = mysql_query("SELECT num FROM numbers ORDER BY num DESC LIMIT 1");

What it does it retrieve the latest entry in the database, which is an int, and I want it to store into the PHP variable $num. Is this possible?

Michael Jarvis
  • 1,115
  • 3
  • 10
  • 17
  • See the manual examples. [`mysql_query`](http://php.net/mysql_query) returns a result handle or something. You need to fetch a row or column. – mario Jan 24 '15 at 13:08

1 Answers1

-1

Is this possible?

Yes, that is possible.

You should, however, use mysqli or PDO instead of the old mysql_ functions.

See: Why shouldn't I use mysql_* functions in PHP?

An example of how it could look:

$result = $mysqli->query("SELECT num FROM numbers ORDER BY num DESC LIMIT 1");
$num = $result->fetch_object()->num; 
Community
  • 1
  • 1
Johann Bauer
  • 2,488
  • 2
  • 26
  • 41