2

This code is supposed to get the user id from my database but it returns this : Resource id #6

So how would I code this to make it return the user id?Here is my code so far and I know that mysql_query is becoming outdated:

<?php
include("login_check.php");
include("dbconnect.php");
mysql_select_db("maxgee_close2");
$username = $_COOKIE['maxgee_me_user']; 
$user_id = mysql_query("select user_id from users where username = '$username'");
echo "$user_id";
?>
3

3 Answers 3

2
mysql_connect("localhost", "mysql_user", "mysql_password") or 
  die("Could not connect: " . mysql_error());
mysql_select_db("maxgee_close2");
$result = mysql_query("select user_id from users where username = '$username'");
$row = mysql_fetch_array($result);
echo $row['user_id'];    
mysql_free_result($result);
Sign up to request clarification or add additional context in comments.

Comments

2

That code it's not supposed to return the ID, but a resource! You then need to access the data in that resource, Read here: http://www.tizag.com/mysqlTutorial/mysqlfetcharray.php (or maybe the official PHP docs)

Comments

2

From the mysql_query() documentation:

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

...

The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.

tl;dr mysql_query returns a resource that you have to pass to another function such as mysql_fetch_array(). The ressource itself doesn't contain your rows.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.