Jump to content

Query a MySQL DB


xProteuSx

Recommended Posts

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]
Link to comment
https://forums.phpfreaks.com/topic/27190-query-a-mysql-db/
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/27190-query-a-mysql-db/#findComment-124344
Share on other sites

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. 
Link to comment
https://forums.phpfreaks.com/topic/27190-query-a-mysql-db/#findComment-124347
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/27190-query-a-mysql-db/#findComment-124351
Share on other sites

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]
Link to comment
https://forums.phpfreaks.com/topic/27190-query-a-mysql-db/#findComment-124477
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.