Forum Moderators: coopster

Message Too Old, No Replies

Retrieve data from mysql field and assign as a variable

I'll be darned if I can figure this out...should be possible tho!

         

imtrypin

7:11 am on Oct 4, 2007 (gmt 0)

10+ Year Member



Hey howdy hey.

Ok, here's the deal. I have written a MySQL database and I'm in the last stages of writing the PHP frontend for it. I need to be able to pull an email address from the database and then send an email to that user by using the PHP mail function.

My database is running perfectly, it can and does return the email when queried, I just can't figure out how make data that is retrieved into a variable.

Here is my code (in it's entirety) for reference:
<?php
$con = mysql_connect('localhost','*********','************');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db('**********');

// Var Notes:
// var = quote number from htm
// var1 = Serach Criteria from htm
// var3 = textarea from php
// Declare vars from searchdb.htm
$variable_from_htm = @$_POST['var'];
$variable_from_htm1 = @$_POST['var1'];
$variable_from_php = @$_POST['body'];

if ($variable_from_htm=='lname')
$query = "select * from customer where lname = '$variable_from_htm1' ";
elseif ($variable_from_htm=='recordnum')
$query = "select * from customer where recordnum = '$variable_from_htm1' ";
elseif ($variable_from_htm=='phone')
$query = "SELECT * FROM customer WHERE phone = '$variable_from_htm1' " ;
else
echo 'Incorrect Syntax: No Criteria Given';

$result = mysql_query($query) or die("Can't execute insert query: " . mysql_error());
while($row = mysql_fetch_array($result))
{
echo '<br />';
echo "Customer's Email Address: ";
echo '<font color="#FF7600">',$row['email'], '</font>';
}

$mailaddy = '$row['email']';

$to = "$mailaddy"; // Seperate With Comma's
$subject = "Test";
$message = "$variable_from_php";

$result = mail($to,"$subject ",$message, "From:PHPMail-Script");
if ($result){
echo ('Mail Sent');
} else { echo('Error Sending Mail'); }

mysql_close($con);

?>

Any and all help is greatly appreciated!
Thank you in advance.

Habtom

7:50 am on Oct 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try the following:

$result = mysql_query($query) or die("Can't execute insert query: " . mysql_error());
while($row = mysql_fetch_array($result))
{
echo '<br />';
echo "Customer's Email Address: ";
echo '<font color="#FF7600">',$row['email'], '</font>';

$mailaddy .= $row['email']." , "; //Change Number 1 - In the loop and correct syntax
}

$mailaddy = rtrim($mailaddy,","); //Change Number 2 - Trimming the extra comma

$to = "$mailaddy"; // Seperate With Comma's
$subject = "Test";
$message = "$variable_from_php";

$result = mail($to,"$subject ",$message, "From:PHPMail-Script");
if ($result){
echo ('Mail Sent');
} else { echo('Error Sending Mail'); }

Habtom

[edited by: Habtom at 7:50 am (utc) on Oct. 4, 2007]

imtrypin

8:24 am on Oct 4, 2007 (gmt 0)

10+ Year Member



Yeah Buddy! That did it!

Thank you very much!

For the life of me, I just couldn't figure that one out!

Habtom

8:26 am on Oct 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not a problem, and Welcome to Webmasterworld.

imtrypin

9:43 am on Oct 4, 2007 (gmt 0)

10+ Year Member



Hey HabTom,
And anybody else who runs into this question. HabTom's help was great, amazingly quick and so very greatly appreciated.

However, I did notice one little problem while setting up another page with a serious amount of variables pulled from the database to be included in the body of the email.

Code:
$lastname .= $row['lname']." , ";

Will produce this output:
Lastname ,

New Code:
$firstname .= $row['fname']."";

Will produce this output:
Firstname

Just in case anybody needs this little tidbit of knowledge that HabTom graciously provided.

Habtom

9:51 am on Oct 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you need to run multiple values comma separated, you might not know when it is exactly ending. So, to keep it simple, after running comma separated, the last one left can be trimmed.

rtrim($mailaddy,",");

rtrim [php.net] is used to trim any character or spaces at the right end of a string.

Habtom

imtrypin

9:57 am on Oct 4, 2007 (gmt 0)

10+ Year Member



Yeah, I gathered that after looking at your code. Maybe I screwed up by entering the variables one line at a time? Not sure about that, I've dinked with PHP over the years but never tried anything on this scale.

I used your code as an example and listed the variables I wanted to retrieve from the database one at a time then did rtrim on them one at a time and ending up with ,'s all over my email! Actually, it was kind of a trip to read...Must be what it's like to try and code while on acid, eh? LOL.

Anyway, I really do appreciate your help and I did get it running thanx to you.