Page 1 of 1
Quick query question
Posted: Thu May 08, 2008 11:43 pm
by latoyale
Hello,
I have a form on my site that users can enter a description into. The information is then emailed to a manager. The email includes the person's description and their username (provided by sessions when they log in)
I want the emailed info into to include the users email address which is in the database. How do I pull that information.
So far I have:
$email = "select email from users where username = '$username'";
Since the username comes from a session is this why it doesn't work?

Re: Quick query question
Posted: Fri May 09, 2008 12:28 am
by califdon
latoyale wrote:Hello,
I have a form on my site that users can enter a description into. The information is then emailed to a manager. The email includes the person's description and their username (provided by sessions when they log in)
I want the emailed info into to include the users email address which is in the database. How do I pull that information.
So far I have:
$email = "select email from users where username = '$username'";
Since the username comes from a session is this why it doesn't work?

No, a variable is a variable.
The line of code you showed does nothing more than assign a string to a variable called $email. To extract data from a database you must first connect to the database server, then select a database, then query the database (using a SQL string such as the one you showed), then extract the desired data from the result set.
You might want to read this:
http://www.php-mysql-tutorial.com/conne ... ng-php.php
Re: Quick query question
Posted: Fri May 09, 2008 1:38 am
by latoyale
Unfortunately I am still having a problem. I know this is a simple syntax problem, I just can't figure out whats going wrong. This code produces the following output:
Description: maybe
From: rjefferson
Email:Array
Its not giving me the email address from the database as I hoped.
<?php
/* Include Files *********************/
session_start();
include("login.php");
include("database.php");
/*************************************/
/* Subject and Email Variables */
$emailsubject = 'Description Form Results';
$webMaster = '
sample@sample.com';
/* Gathering Data Variable */
$description = $_POST['description'];
$username = $_SESSION['username'];
$result = mysql_query("SELECT email from users where username = '$username'")
or die(mysql_error());
echo "<pre>$result</pre>";
$email = mysql_fetch_array( $result );
$body = <<<EOD
<br><hr><br>
Description: $description <br><br>
From: $username <br><br>
Email:$email <br><br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailsubject, $body, $headers);
/* Results rendered as HMTL */
$theResults = <<<EOD
<html>
<head><meta http-equiv="Refresh"content="99;url=login_success.php">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Thank you</title>
</head>
<body><div>
<div align="center"> Thank you for submitting your form<br></div>
</body>
</html>
EOD;
echo "$theResults";
?>
Re: Quick query question
Posted: Fri May 09, 2008 12:23 pm
by califdon
latoyale wrote:
Email:Array
[/b]
Its not giving me the email address from the database as I hoped.
Code: Select all
<?php
$result = mysql_query("SELECT email from users where username = '$username'")
or die(mysql_error());
echo "<pre>$result</pre>";
[color=#FF4000]$email = mysql_fetch_array( $result );[/color]
$body = <<<EOD
<br><hr><br>
Description: $description <br><br>
From: $username <br><br>
Email:$email <br><br>
EOD;
mysql_query() doesn't return a simple string, it returns a "resource", which means it is just a pointer to what may in some cases be an array with thousands of indexes. You have to extract the individual variables (strings or numbers)from it before you can use them. Actually, I don't see how you were able to see the description and username at all. You must do something like:
Code: Select all
$row = mysql_fetch_array($result);
$description = $row['description'];
$username = $row['username'];
$email = $row['email'];
where each variable is addressed as an index of the array $row.
Re: Quick query question
Posted: Mon May 12, 2008 4:06 am
by latoyale
Although I am trying very hard to understand this I am still having problems. My new output is:
SELECT email FROM users WHERE username = 'rjefferson'
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/j/a/b/jaberkjaber/html/askslicky/inner/description.php on line 24
Thank you for submitting your form
I added in the row feature to get code to produce the correct results. I never knew getting an email address from a database, based on an unique username could be so difficult.

Please help.
Code: Select all
<?php
/* Include Files *********************/
session_start();
include("login.php");
include("database.php");
/*************************************/
/* Subject and Email Variables */
$emailsubject = 'Description Form Results ';
$webMaster = 'email@email.com';
/* Gathering Data Variable */
$description = $_POST['description'];
$username = $_SESSION['username'];
$result ="SELECT email FROM users WHERE username = '$username'"
or die(mysql_error());
echo "<pre>$result</pre>";
$row = mysql_fetch_array($result);
$username = $row['username'];
$email = $row['email'];
$body = <<<EOD
<br><hr><br>
Description: $description <br><br>
From: $username <br><br>
Email:$email <br><br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailsubject, $body, $headers);
/* Results rendered as HMTL */
$theResults = <<<EOD
<html>
<head><meta http-equiv="Refresh"content="99;url=login_success.php">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Thank you </title>
</head>
<body><div>
<div align="center"> Thank you for submitting your form<br></div>
</body>
</html>
EOD;
echo "$theResults";
?>
Re: Quick query question
Posted: Mon May 12, 2008 2:41 pm
by califdon
latoyale wrote:Although I am trying very hard to understand this I am still having problems. My new output is:
SELECT email FROM users WHERE username = 'rjefferson'
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/j/a/b/jaberkjaber/html/askslicky/inner/description.php on line 24
Thank you for submitting your form
I added in the row feature to get code to produce the correct results. I never knew getting an email address from a database, based on an unique username could be so difficult.

Please help.
Code: Select all
<?php
/* Include Files *********************/
session_start();
include("login.php");
include("database.php");
/*************************************/
/* Subject and Email Variables */
$emailsubject = 'Description Form Results ';
$webMaster = 'email@email.com';
/* Gathering Data Variable */
$description = $_POST['description'];
$username = $_SESSION['username'];
[color=#FF0000]$result ="SELECT email FROM users WHERE username = '$username'"
or die(mysql_error());[/color]
echo "<pre>$result</pre>";
$row = mysql_fetch_array($result);
$username = $row['username'];
$email = $row['email'];
Well, now you have omitted the
mysql_query() function that produces the $result.
You really need to learn basic PHP syntax when working with a database. The following steps must occur, and in this order:
- Connect to the MySQL server: mysql_connect([host],[user],[password]) or die(mysql_error());
- Select the database: mysql_select_db([database name]) or die(mysql_error());
- Execute the query and assign result set pointer to a variable: $result=mysql_query([SQL statement]) or die(mysql_error());
- Fetch row(s) from result set: $row=mysql_fetch_array($result):
- Use data in fields of the row: $email=$row['email']; or whatever