-2

So I have the code below (and at the top the database connector and session start). However, he doesn't echo the amount of cubes (a valuta on my site).

$cubes = mysql_query("SELECT cubes FROM leden WHERE id='" . $_SESSION['id'] . "'");

echo "You currently have " . $cubes;

What is going wrong here? The database is there, the tables and everything exist.

  • 1
    *"What is going wrong here?"* - Consult these following links http://php.net/manual/en/function.mysql-error.php and http://php.net/manual/en/function.error-reporting.php and apply that to your code and it will tell you. – Funk Forty Niner Feb 22 '16 at 15:33
  • 2
    Executing a query returns a resultset, which can't be displayed simply by doing an echo; you then need to fetch each record returned in that resultset, and display the values returned..... as even the worst tutorials and examples will tell you – Mark Baker Feb 22 '16 at 15:34
  • But if you're just learning how to use PHP and databases, I'd strongly recommend switching to the MySQLi or PDO extensions (rather than using the old, deprecated MySQL extension that has been dropped from the latest versions of PHP), and learning to use prepared statements with bind variables – Mark Baker Feb 22 '16 at 15:35
  • @MarkBaker What do you mean? Is MySQL going to be deleted ? all this work for nothing ?? – Jake Carney Feb 22 '16 at 15:37
  • 1
    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 Feb 22 '16 at 15:38
  • MySQL is not being deleted from PHP, there are just newer, safer methods for interacting with databases. – Jay Blanchard Feb 22 '16 at 15:39
  • 1
    The MySQL extension has already been dropped from the latest version of PHP; in favour of the MySQLi or PDO extensions – Mark Baker Feb 22 '16 at 15:40
  • I was going to *vote to close* but there doesn't appear to be an RTFM option for the reason... – CD001 Feb 22 '16 at 15:44

1 Answers1

0

The mysql_query function returns you a result set. So if you want to get the information you must also do :

$cubesRow = mysql_fetch_object($cubes);
echo "You currently have " . $cubesRow->cubes;