1

I am trying to display the ammouth of credits this individual has. But it keeps coming up with "Resource id #6", now ive been hitting google and stackoverflow, and i see people fixing it for others, but they dont really explain much...? The database name is test, members are in users, and users have a table called credits

<?php
session_start();
include_once 'dbconnect.php';

if(!isset($_SESSION['user']))
{
 header("Location: index.php");
}
$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);

// Trying to display the ammouth of credits from this particular member...

$credits=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
echo $credits;
?>
kobar1990
  • 13
  • 8
  • I now see this copy paste from my file just selects users, ive tried stuff like select credits from users where user_id="..." – kobar1990 Jan 02 '16 at 16:04
  • the result of a `mysql_query()` call is a ResultSet and not an array or so. You have to parse ResultSet, e.g., using `mysql_fetch_assoc()` like you did with the query before. You should however, switch to PDO or mysqli anyways, as `mysql_x` functions are deprecated and removed by version 7 of PHP. – Sirko Jan 02 '16 at 16:06
  • The result from your query is an object of type `resource` - you need to retrieve the records from the object in the appropriate manner. Also, why two identical queries? – Professor Abronsius Jan 02 '16 at 16:06

1 Answers1

0

Why are you executing the same exact query twice?

Here you get the results into an array, which you can use to see the values:

$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);

But then here you expect the exact same operation to return a string instead of a resource:

$credits=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
echo $credits;

That query return a resouce, not a string. So the echo statement does its best to represent that resource, which is exactly what you're seeing.

Just get the value from $userRow, since you already queried the database:

echo $userRow['credits']; // or whatever the column is called

Note: You should really consider upgrading from the mysql_* operations to mysqli_* or PDO, and take advantage of prepared statements and query parameters. mysql_* is deprecated and no longer supported. And you may have a potential SQL injection vulnerability in your query where you should be using a query parameter instead (depending on where $_SESSION['user'] gets its value).

David
  • 208,112
  • 36
  • 198
  • 279