h1234 Posted October 2, 2013 Share Posted October 2, 2013 function user_data($user_id){ Global $dbc; $data=array(); $user_id = (int)$user_id; $func_num_args=func_num_args(); $func_get_args=func_get_args(); if($func_num_args >=1){ unset($func_get_args[0]); $fields=' ` '.implode(' ` , ` ' , $func_get_args).' ` '; $data=mysqli_query($dbc," SELECT '$fields' FROM `users` WHERE `user_id` = '$user_id'"); //mysqli_error($dbc); if ($data === false) { die("error on mysqli_query: ". mysqli_error($dbc)); } $data=mysqli_fetch_assoc($data); print_r ($data); //return $data; } } function logged_in(){ return(isset($_SESSION['user_id'])) ? true :false; } when i print above to screen i get this below Array ( [` user_id ` , ` username ` , ` password ` , ` name ` , ` surname ` , ` email ` ] => ` user_id ` , ` username ` , ` password ` , ` name ` , ` surname ` , ` email ` ) when i change the $fields in the in the query to * it displays the correct info but when i use the file below to echo a the user_id nothing echo's out this is the file that suppose to work but it doe not echo anything . this file is called init.php contains all functions ect. <?php session_start(); require('connect.php'); require('functions/general.php'); require('functions/users.php'); // to be accessed by every page to check errors for login if(logged_in()===true){ $session_user_id=$_SESSION['user_id']; $user_data = user_data($session_user_id,'user_id','username','password', 'name' , 'surname' ,'email' ); echo $user_data['user_id']; } else echo "cannot retreive data"; Quote Link to comment https://forums.phpfreaks.com/topic/282635-cannot-view-the-fields-unless-i-state-select-all-but-it-still-does-not-work/ Share on other sites More sharing options...
Ch0cu3r Posted October 2, 2013 Share Posted October 2, 2013 (edited) This is not a PHP Math problem posted in wrong place. But to answer you question There are two problems with the user_data function, You're not constructing the SQL query properly $data=mysqli_query($dbc," SELECT '$fields' FROM `users` WHERE `user_id` = '$user_id'"); Remove the quotes around $fields, this will treat the value of $fields as a string not as individual fields listed within $fields. The other problem is you need to uncomment (remove the //) the last line in user_data function //return $data; Now your script should work as you expect. Also please post your code within code tags (by hitting the <> button in the reply box) This makes your code alot clear to read Edited October 2, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282635-cannot-view-the-fields-unless-i-state-select-all-but-it-still-does-not-work/#findComment-1452179 Share on other sites More sharing options...
h1234 Posted October 2, 2013 Author Share Posted October 2, 2013 This is not a PHP Math problem posted in wrong place. But to answer you question There are two problems with the user_data function, You're not constructing the SQL query properly $data=mysqli_query($dbc," SELECT '$fields' FROM `users` WHERE `user_id` = '$user_id'"); Remove the quotes around $fields, this will treat the value of $fields as a string not as individual fields listed within $fields. The other problem is you need to uncomment (remove the //) the last line in user_data function //return $data; Now your script should work as you expect. Also please post your code within code tags (by hitting the <> button in the reply box) This makes your code alot clear to read thanks alot but it now says error on mysqli_query: Unknown column ' user_id ' in 'field list' Quote Link to comment https://forums.phpfreaks.com/topic/282635-cannot-view-the-fields-unless-i-state-select-all-but-it-still-does-not-work/#findComment-1452197 Share on other sites More sharing options...
Ch0cu3r Posted October 2, 2013 Share Posted October 2, 2013 (edited) You have spaces padding your field names. This is because of this line $fields=' ` '.implode(' ` , ` ' , $func_get_args).' ` '; It is padding your field names as `(space)field_name(space)`. Change the above line to $fields='`'.implode('`, `', $func_get_args).'`'; Edited October 2, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282635-cannot-view-the-fields-unless-i-state-select-all-but-it-still-does-not-work/#findComment-1452200 Share on other sites More sharing options...
cyberRobot Posted October 2, 2013 Share Posted October 2, 2013 Try removing the space before and after the column names. So instead of ` user_id `...it should probably be `user_id`. Quote Link to comment https://forums.phpfreaks.com/topic/282635-cannot-view-the-fields-unless-i-state-select-all-but-it-still-does-not-work/#findComment-1452201 Share on other sites More sharing options...
Barand Posted October 2, 2013 Share Posted October 2, 2013 Field names shouldn't be in single quotes anyway. Quote Link to comment https://forums.phpfreaks.com/topic/282635-cannot-view-the-fields-unless-i-state-select-all-but-it-still-does-not-work/#findComment-1452203 Share on other sites More sharing options...
cyberRobot Posted October 2, 2013 Share Posted October 2, 2013 Field names shouldn't be in single quotes anyway. Are you referring to the backticks in the code below? $fields='`'.implode('`, `', $func_get_args).'`'; If so, they're necessary if a column is named "desc"...or one of the other reserved words in MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/282635-cannot-view-the-fields-unless-i-state-select-all-but-it-still-does-not-work/#findComment-1452211 Share on other sites More sharing options...
h1234 Posted October 2, 2013 Author Share Posted October 2, 2013 Are you referring to the backticks in the code below? $fields='`'.implode('`, `', $func_get_args).'`'; If so, they're necessary if a column is named "desc"...or one of the other reserved words in MySQL. thanks it works now i see the correct field names and the values from the database but i cannot echo out the different field names , in this section nothing appears at all i been trying for over hour if(logged_in()===true){ $session_user_id=$_SESSION['user_id']; $user_data = user_data($session_user_id,'user_id','username','password','name' ,'surname' ,'email'); echo $user_data['user_id']; } else echo "cannot retreive data"; Quote Link to comment https://forums.phpfreaks.com/topic/282635-cannot-view-the-fields-unless-i-state-select-all-but-it-still-does-not-work/#findComment-1452213 Share on other sites More sharing options...
Ch0cu3r Posted October 2, 2013 Share Posted October 2, 2013 (edited) In user_data() function you did uncomment this line //return $data; By removing the // in front of it Edited October 2, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/282635-cannot-view-the-fields-unless-i-state-select-all-but-it-still-does-not-work/#findComment-1452215 Share on other sites More sharing options...
h1234 Posted October 2, 2013 Author Share Posted October 2, 2013 In user_data() function you did uncomment this line //return $data; By removing the // in front of it AWSOME!! YOUR ARE. all of you thanks alot!!! Quote Link to comment https://forums.phpfreaks.com/topic/282635-cannot-view-the-fields-unless-i-state-select-all-but-it-still-does-not-work/#findComment-1452221 Share on other sites More sharing options...
cyberRobot Posted October 2, 2013 Share Posted October 2, 2013 I have marked the topic as solved. If you need anything else, please mark it as unsolved...or start a new topic. Quote Link to comment https://forums.phpfreaks.com/topic/282635-cannot-view-the-fields-unless-i-state-select-all-but-it-still-does-not-work/#findComment-1452223 Share on other sites More sharing options...
Barand Posted October 2, 2013 Share Posted October 2, 2013 Are you referring to the backticks in the code below? $fields='`'.implode('`, `', $func_get_args).'`'; If so, they're necessary if a column is named "desc"...or one of the other reserved words in MySQL. Must clean my glasses! Quote Link to comment https://forums.phpfreaks.com/topic/282635-cannot-view-the-fields-unless-i-state-select-all-but-it-still-does-not-work/#findComment-1452233 Share on other sites More sharing options...
h1234 Posted October 4, 2013 Author Share Posted October 4, 2013 (edited) I have marked the topic as solved. If you need anything else, please mark it as unsolved...or start a new topic. another problem has come up for me, i am abit clueless now. i registered another user on the data base but all the database is recognizing is my user id not the new one . the new one logs registers and logs in on the page but it does not display correct username , it thinks it is mine all the time function user_data($user_id){ Global $dbc; $data=array(); $user_id = (int)$user_id; $func_num_args=func_num_args(); $func_get_args=func_get_args(); if($func_num_args >=1){ unset($func_get_args[0]); $fields='`'.implode('`, `', $func_get_args).'`'; $data=mysqli_query($dbc," SELECT $fields FROM `users` WHERE `user_id` = $user_id "); //mysqli_error($dbc); if ($data === false) { die("error on mysqli_query: ". mysqli_error($dbc)); } $data=mysqli_fetch_assoc($data); //print_r ($data); return $data; } } function logged_in(){ GLOBAL $dbc; return(isset($_SESSION['user_id'])) ? true : false; } function user_exists($username){ GLOBAL $dbc; $username=sanatize($username); $query= mysqli_query($dbc,"SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' "); $check= mysqli_fetch_array( $query , MYSQLI_BOTH); return ($check[0]==1)?true:false; } function email_exists($email){ GLOBAL $dbc; $email=sanatize($email); $query= mysqli_query($dbc,"SELECT COUNT(`user_id`) FROM `users` WHERE `email` = '$email' "); $check= mysqli_fetch_array( $query , MYSQLI_BOTH); return ($check[0]==1)?true:false; } //its suppose to count the user_id but count didnt work here i am not sure why function user_id_from_username($username){ GLOBAL $dbc; $username = sanatize($username); $query= mysqli_query($dbc,"SELECT `user_id` FROM `users` WHERE `username` = '$username' "); $check= mysqli_fetch_array( $query , MYSQLI_BOTH); return $check[0]=='user_id'; } Edited October 4, 2013 by h1234 Quote Link to comment https://forums.phpfreaks.com/topic/282635-cannot-view-the-fields-unless-i-state-select-all-but-it-still-does-not-work/#findComment-1452538 Share on other sites More sharing options...
Ch0cu3r Posted October 4, 2013 Share Posted October 4, 2013 Please when posting code, paste it into tags (click the <> button before posting message). How you should log in a user is to query the database with the user/password credentials they provided. The query should retrieve all their data you need when the username/password matches. Example query would be SELECT user_id, password, email, time_of_last_login, access_lvl, etc.. FROM users_table WHERE username=$username AND password=$password When the username/password matches it'll return all the info in the SELECT clause. You store this information to the $_SESSION variable. You should not be having to re-query the database to get more information about them. To see if they are logged in you just checked the $_SESSION variable validates to your requirements. The basic way to do this is to set a $_SESSION['is_logged_in'] variable to true when the user successfully logs in. For any page that requires the user to be logged in you check this variable is true eg <?php session_start(); if(isset($_SESSION['is_logged_in']) && $_SESSION['is_logged_in'] === true) { // user is logged in display page } else { // user is not logged in // display error and login form } When logging users out you just clear the $_SESSION variable with unset. To completly remove the session you'd use session_destroy. Quote Link to comment https://forums.phpfreaks.com/topic/282635-cannot-view-the-fields-unless-i-state-select-all-but-it-still-does-not-work/#findComment-1452545 Share on other sites More sharing options...
h1234 Posted October 8, 2013 Author Share Posted October 8, 2013 (edited) Please when posting code, paste it into tags (click the <> button before posting message). How you should log in a user is to query the database with the user/password credentials they provided. The query should retrieve all their data you need when the username/password matches. Example query would be SELECT user_id, password, email, time_of_last_login, access_lvl, etc.. FROM users_table WHERE username=$username AND password=$password When the username/password matches it'll return all the info in the SELECT clause. You store this information to the $_SESSION variable. You should not be having to re-query the database to get more information about them. To see if they are logged in you just checked the $_SESSION variable validates to your requirements. The basic way to do this is to set a $_SESSION['is_logged_in'] variable to true when the user successfully logs in. For any page that requires the user to be logged in you check this variable is true eg <?php session_start(); if(isset($_SESSION['is_logged_in']) && $_SESSION['is_logged_in'] === true) { // user is logged in display page } else { // user is not logged in // display error and login form } When logging users out you just clear the $_SESSION variable with unset. To completly remove the session you'd use session_destroy. thanks alot but when i log in with user 2 it recognizes but it does not recognise the users id so it displays as user 1 , for example when i echo the user id to say "hello" its suppose to say "hello user2" but always displays user1 name. like <?php echo $user_data['name'];?> this does not work. I am not sure why. i cant set session again or it gives an error that the sesssion is already set Edited October 8, 2013 by h1234 Quote Link to comment https://forums.phpfreaks.com/topic/282635-cannot-view-the-fields-unless-i-state-select-all-but-it-still-does-not-work/#findComment-1453027 Share on other sites More sharing options...
Ch0cu3r Posted October 8, 2013 Share Posted October 8, 2013 i cant set session again or it gives an error that the sesssion is already set Sounds like you're calling session_start() multiple times. You only need to call this function once to start the session not each time you need to modify the $_SESSION vars. You can set or reset the $_SESSION variables as many times as you like. Quote Link to comment https://forums.phpfreaks.com/topic/282635-cannot-view-the-fields-unless-i-state-select-all-but-it-still-does-not-work/#findComment-1453030 Share on other sites More sharing options...
h1234 Posted October 9, 2013 Author Share Posted October 9, 2013 (edited) Sounds like you're calling session_start() multiple times. You only need to call this function once to start the session not each time you need to modify the $_SESSION vars. You can set or reset the $_SESSION variables as many times as you like. thanks so u suggest i save session start in a different file? as i have it in my confiq file with my database connections , functions ect. Edited October 9, 2013 by h1234 Quote Link to comment https://forums.phpfreaks.com/topic/282635-cannot-view-the-fields-unless-i-state-select-all-but-it-still-does-not-work/#findComment-1453238 Share on other sites More sharing options...
Barand Posted October 9, 2013 Share Posted October 9, 2013 (edited) Just make sure you call it once right at the top of each script that uses session variables Edited October 9, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/282635-cannot-view-the-fields-unless-i-state-select-all-but-it-still-does-not-work/#findComment-1453241 Share on other sites More sharing options...
h1234 Posted October 9, 2013 Author Share Posted October 9, 2013 (edited) Just make sure you call it once right at the top of each script that uses session variables thanks so this would solve my problem of the user ID always shows 1 it never shows user 2 for example. it recognizes the user exists but does not recognize that specific users name or id. i get this error Fatal error: Call to undefined function logged_in() Edited October 9, 2013 by h1234 Quote Link to comment https://forums.phpfreaks.com/topic/282635-cannot-view-the-fields-unless-i-state-select-all-but-it-still-does-not-work/#findComment-1453244 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.