Adam93 Posted September 5, 2014 Share Posted September 5, 2014 Hi, So I'm not very familiar with using mySQLi, but I'm wanting to print a user's last name, depending on which user is logged in (obviously it needs to be their last name and not another users) So, we're getting the session for the user and saving their username as $username $user = Session::Get('current_user'); $username = $user->Get('username'); And then my query to display their lastname? $result = $db->Select('lastname')->Where('username', '$username')->Get(Config::Get('db.table')); print_r($result) But the query doesn't work, no error? Forgive my ignorance! >.< Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 5, 2014 Share Posted September 5, 2014 Variables are not expanded in single quotes. Remove the single quotes around the use of $username here ...Where('username', '$username')... Quote Link to comment Share on other sites More sharing options...
Adam93 Posted September 5, 2014 Author Share Posted September 5, 2014 Thanks, but it's now displaying; Array ( [0] => Array ( [0] => 0 [lastname] => 0 ) ) Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 5, 2014 Share Posted September 5, 2014 Is the $username set? echo "Username is: $username"; Quote Link to comment Share on other sites More sharing options...
Adam93 Posted September 5, 2014 Author Share Posted September 5, 2014 Whoops, so, cleared my sessions and logged in again, now it's displaying; Array ( [0] => Array ( [0] => frank [lastname] => frank ) ) 'frank' is the information in the table, which is correct, but I only want to display that piece of information, not "Array ( [0] => Array ( [0] =>"... Any ideas? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 5, 2014 Share Posted September 5, 2014 (edited) Change print_r($result) to echo $result['lastname'] Edited September 5, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Adam93 Posted September 5, 2014 Author Share Posted September 5, 2014 Now I'm not getting anything Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted September 5, 2014 Solution Share Posted September 5, 2014 Oh, sorry try echo $result[0]['lastname'] Quote Link to comment Share on other sites More sharing options...
Adam93 Posted September 5, 2014 Author Share Posted September 5, 2014 Thank you! Works fine now! Just a quick question for future, why the '[0]'? :-) Quote Link to comment Share on other sites More sharing options...
CroNiX Posted September 5, 2014 Share Posted September 5, 2014 Because it's an array within an array, so you need to access the first element (0), which gives you the array with your actual values. Quote Link to comment Share on other sites More sharing options...
Adam93 Posted September 5, 2014 Author Share Posted September 5, 2014 Ahh, thanks for you help! Quote Link to comment 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.