xProteuSx Posted November 14, 2006 Share Posted November 14, 2006 I am trying to output the 'users_email' column for user $loginusername from the table 'users'. The code is below. The query consistently dies (ie. Shits its pants). I don't know what I am doing wrong. Thanks for the help.[code]$emailquery = "SELECT 'users_email' FROM 'users' WHERE 'users_handle' LIKE '$loginusername'";$email_result = mysql_query($emailquery) or die ("Query shit its pants!");$email = $email_result;echo "<p>$email</p>";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/27190-query-a-mysql-db/ Share on other sites More sharing options...
Psycho Posted November 14, 2006 Share Posted November 14, 2006 Instead of trying to be qute, why don't you actually use the error handling for what it's for?Change this line:[code]$email_result = mysql_query($emailquery) or die ("Query shit its pants!");[/code]To this:[code]$email_result = mysql_query($emailquery) or die ("The query " . $emailquery . "caused the following error:<br><br>" . mysql_error());[/code]And then it might actually tell you something usefull instead of the "crap" it's showing you now. Quote Link to comment https://forums.phpfreaks.com/topic/27190-query-a-mysql-db/#findComment-124344 Share on other sites More sharing options...
xProteuSx Posted November 14, 2006 Author Share Posted November 14, 2006 This is my first day of PHP and MySQL, so please excuse the fact that I still make mistakes or don't know how to throw in an error output clause. Thanks for the help, though. My code is now:[code]$emailquery = "SELECT users_email FROM users WHERE users_handle LIKE Proteus";$email_result = mysql_query($emailquery) or die ("The query " . $emailquery . " caused the following error:<br><br>" . mysql_error());$email_result = $email;echo "<p>E-Mail: $email</p>";[/code]The output for the error is as follows:[code]The query SELECT users_email FROM users WHERE users_handle LIKE Proteus caused the following error:Unknown column 'Proteus' in 'where clause'[/code]Why is the query treating 'Proteus' as a column? It is an entry in the 'users_handle' column; this is the same row from which I am trying to output the 'users_email' value. Quote Link to comment https://forums.phpfreaks.com/topic/27190-query-a-mysql-db/#findComment-124347 Share on other sites More sharing options...
xProteuSx Posted November 14, 2006 Author Share Posted November 14, 2006 The following code does not issue an error, but the output is blank:[code]$emailquery = "SELECT users_email FROM users";$email_result = mysql_query($emailquery) or die ("The query caused the following error:<br><br>" . mysql_error());$email_result == $email;echo "<p>E-Mail: $email</p>";[/code]'$email' must be coming through as blank for some reason, though the field 'users-email' (there is only one row) is populated. Quote Link to comment https://forums.phpfreaks.com/topic/27190-query-a-mysql-db/#findComment-124351 Share on other sites More sharing options...
Psycho Posted November 14, 2006 Share Posted November 14, 2006 In the first code the problem is that 'Proteus' is not enclosed in single quote in the sql statement. Change the sql query to this:[code]$emailquery = "SELECT users_email FROM users WHERE users_handle LIKE 'Proteus'";[/code]however, that query will only find results where 'users_handle' IS 'Proteus', however the LIKE allows the the matches to be case INsensitive. If you want results where 'Proteus' appears anywhere in users_handle then you would use this:[code]$emailquery = "SELECT users_email FROM users WHERE users_handle LIKE '%Proteus%'";[/code]in your second error you have not propery retrieved the values from the query. You need to do something like this:[code]$emailquery = "SELECT users_email FROM users";$result = mysql_query($emailquery) or die ("The query caused the following error:<br><br>" . mysql_error());while ($row = mysql_fetch_assoc($result)) { echo "<p>E-Mail: $row['users_email']</p><br>";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/27190-query-a-mysql-db/#findComment-124477 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.