waddledoo Posted December 4, 2011 Share Posted December 4, 2011 I am trying to get specific data from a MySQL table but cannot seem to find a proper command. $link = mysqli_connect('#####', '########', '########'); $user = $_SESSION['username']; $email = mysqli_query($link,"select email from members where username='$user'"); echo $email; Specifically, I am trying to get the email address stored for the user specified by the session, and display that address. However, I do not get any output with the current code, and am not sure what to try next. I have tried mysqli_fetch_row() but it only returns an error. Quote Link to comment https://forums.phpfreaks.com/topic/252424-getting-specific-data-from-a-mysql-table/ Share on other sites More sharing options...
Pandemikk Posted December 4, 2011 Share Posted December 4, 2011 $email = mysqli_query($link,"SELECT email FROM members WHERE username= '$user' LIMIT 1"); echo $email['email']; This should work. I don't know if your link values are correct or how your table is designed. I also don't know if $user is the username properly escaped as it should be since you're using it in a query. Quote Link to comment https://forums.phpfreaks.com/topic/252424-getting-specific-data-from-a-mysql-table/#findComment-1294169 Share on other sites More sharing options...
waddledoo Posted December 4, 2011 Author Share Posted December 4, 2011 <?php session_start(); $link = mysqli_connect('#####', '#####', '#####'); $user = $_SESSION['username']; $email = mysqli_query($link,"SELECT email FROM members WHERE username= '$user' LIMIT 1"); echo $email['email']; if ($email=='') {echo "<no address set>";} else {echo '$email';} ?> I figured it would be best to show the complete code. I implemented the changes you suggested, but my output remains the same (nothing). Quote Link to comment https://forums.phpfreaks.com/topic/252424-getting-specific-data-from-a-mysql-table/#findComment-1294177 Share on other sites More sharing options...
Pandemikk Posted December 4, 2011 Share Posted December 4, 2011 I don't see where $user is getting assigned anything? Can you show me where $user is assigned a value? (If it doesn't have a value that's why you're not getting any results!) Use this to show an error message: if (!$email = mysqli_query($link,"SELECT email FROM members WHERE username= '$user' LIMIT 1")) { die('Invalid user specified.'); } Quote Link to comment https://forums.phpfreaks.com/topic/252424-getting-specific-data-from-a-mysql-table/#findComment-1294178 Share on other sites More sharing options...
waddledoo Posted December 4, 2011 Author Share Posted December 4, 2011 The $user is set here $user = $_SESSION['username']; I added the if statement with the die command, and it executes. It seems the user is not being set correctly EDIT: I forgot to set the database earlier in the code. This has solved the error of not returning a result, but now I get the following error: Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\(etc etc...) on line 75 I tried using $display = (string)$email; echo $display; But it gives the same error. Quote Link to comment https://forums.phpfreaks.com/topic/252424-getting-specific-data-from-a-mysql-table/#findComment-1294416 Share on other sites More sharing options...
Pikachu2000 Posted December 4, 2011 Share Posted December 4, 2011 You haven't used one of the mysqli_fetch functions. Your variable holds a result resource, not a value. Quote Link to comment https://forums.phpfreaks.com/topic/252424-getting-specific-data-from-a-mysql-table/#findComment-1294418 Share on other sites More sharing options...
waddledoo Posted December 4, 2011 Author Share Posted December 4, 2011 Alright.. please explain one of these fetch functions Quote Link to comment https://forums.phpfreaks.com/topic/252424-getting-specific-data-from-a-mysql-table/#findComment-1294424 Share on other sites More sharing options...
waddledoo Posted December 5, 2011 Author Share Posted December 5, 2011 I looked up the fetch functions, and after alot of trial and error I came up with: <?php session_start(); $link = mysqli_connect('#####', '#####', '#####'); $user = $_SESSION['username']; mysqli_select_db($link,"#####"); $result = mysqli_query($link,"SELECT email FROM members WHERE username= '$user' LIMIT 1"); $email = mysqli_fetch_row($result); if ($email[0]=='') {echo "<no address set>";} else {echo $email[0];} ?> It works perfectly. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/252424-getting-specific-data-from-a-mysql-table/#findComment-1294473 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.