-1

I've read at least 50 questions & answers by this time about the issue i posted in my question with no luck.

I'm suddenly encountering this issue on my server. 'Suddenly', because i hadn't been encountering any problem about this issue

I use the following query to display the username of logged user. The session starts only when the users submit the login form.

mysql_select_db($database_yyy, $zzz);
$query_people = "SELECT users.id, users.full_name FROM users WHERE users.full_name = '$_SESSION[user_id]'";
$people = mysql_query($query_people, $zzz) or die(mysql_error());
$row_people = mysql_fetch_assoc($people);
$totalRows_people = mysql_num_rows($people);

and the server omits the following notice:

Notice: Undefined index: user_id in /home/user/public_html/testdirectory/test.php on line 75

For example, here line 75 demonstrates the following query:

$query_people = "SELECT users.id, users.full_name FROM users WHERE users.full_name = '$_SESSION[user_id]'";

By the way, there isn't any problem with database connection.

Any idea or resolution?

Thanks,

Klanto Aguntuk
  • 719
  • 1
  • 17
  • 44
  • 2
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Mar 18 '14 at 09:21
  • This is a PHP notice, not a MySQL notice. – Gumbo Mar 18 '14 at 09:21
  • Use mysqli_* or PDO instead of using mysql_* functions(deprecated) – Krish R Mar 18 '14 at 09:21
  • 1
    The error message says that the index "user" doesn't exist, but you aren't trying to use that in any of the code you've shared. – Quentin Mar 18 '14 at 09:21
  • I think you've counted wrong, or the server is using an outdated version of the code. Probably you are referencing `$row['user']` somewhere outside of the snippet you posted. – GolezTrol Mar 18 '14 at 09:27
  • @Gumbo , would you read the question carefully? Have i said anywhere it's a mysql notice? – Klanto Aguntuk Mar 18 '14 at 09:31
  • @SilentPond No, but you had tagged it with [tag:mysql]. – Gumbo Mar 18 '14 at 09:31
  • @Gumbo isn't the notice caused by mysql query? can't i tag it with mysql? what do you think? – Klanto Aguntuk Mar 18 '14 at 09:33
  • @SilentPond The notice was raised because you’re trying to read an entry from an array that doesn’t exist. What you do with that value is irrelevant. Just because you want to use in a MySQL query doesn’t make it relevant to be tagged with [tag:mysql]. That’s why I removed the tag. – Gumbo Mar 18 '14 at 09:35
  • @Gumbo that's ok. but we are going away from the main topic. – Klanto Aguntuk Mar 18 '14 at 09:44

1 Answers1

0
var_dump($_SESSION);

If there is no user_id key->value pair, then double check if you got session_start() at the top of php file.

Also, use $_SESSION['user_id'] with quotes

Nizam
  • 5,698
  • 9
  • 45
  • 57
michal.hubczyk
  • 698
  • 3
  • 9
  • `user_id` was initiated in a session for future use. it shall work only after the users log in. so i can say it without dumping the session that user_id key won't exist unless the users log in. yes, `session_start()` was called in an include file before the mysql query. any idea how do i do it then? – Klanto Aguntuk Mar 18 '14 at 09:40
  • If I understand correctly, you are getting this notice, when user is not logged in -> that's expected behavior. – michal.hubczyk Mar 18 '14 at 09:43
  • thanks again for being relevant. does it impact the functionality of purpose? can i do the same thing in a correct grammatical way? – Klanto Aguntuk Mar 18 '14 at 09:48
  • Use (isset) if(isset($_SESSION['user_id'])) { // actions and/or mysql queries of logged users // but you will need to initialize $people, $row_people and $totalRows_people with empty values/arrays if you are accessing them } if you want quick fix then change your SQL to something like: $query_people = "SELECT users.id, users.full_name FROM users WHERE users.full_name = isset($_SESSION[user_id]) ? '$_SESSION[user_id]' : null"; but I'd not recommend this – michal.hubczyk Mar 18 '14 at 09:56
  • yes when i received the warning i thought of the same resolution but can you imagine how lengthy it shall be to define each and variable separately? that doesn't seem practical to me. it seems that there is no correct way to get rid from this notice. let me check the quick fix though i'm not familiar at all with the fix you suggested. – Klanto Aguntuk Mar 18 '14 at 10:06
  • `$_SESSION['user_id']` `(quoted key)` cause `Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING` on my server. – Klanto Aguntuk Mar 18 '14 at 10:10
  • 1
    $query_people = "SELECT users.id, users.full_name FROM users WHERE users.full_name = '".(isset($_SESSION['user_id']) ? $_SESSION['user_id'] : "")."'"; it just checks for full_name = '', it will work if you don't have any rows with empty full_name. checked it, however it is better to check if variable was initialized before. – michal.hubczyk Mar 18 '14 at 10:55