0

I am trying a lot but nothing work for me.

My query is that how to fetch column data from sql database Table and put that data into an string in PHP?

Please check What I am doing.

I have the table "DriveRegisterTable" in which 4 columns in database table, 1st "id" 2nd "username" 3rd "txtMail" 4th "rxtMail" .

Now I want to select txtMail and rxtMail from database table where username = "John"

Now in my database table "username" john have txtMail is " [email protected]" and rxtMail is "[email protected]"

Now I want to fetch these thing on my php string.

Now I want to select txtMail and rxtMail from database table where username = "John" and put txtMail and rxtMail values which are [email protected] and [email protected] in PHP String and then send the mail.

Please check what I am doing it is not working for me:

<?php


$db_host  = "localhost";
$username = "jacktask";
$db_pass  = "sormor4455";
$database = "myDatabaseDB";

mysql_connect("$db_host", "$username", "$db_pass");

@mysql_select_db($database) or die("Unable to find database4");

$txt_username          = $_REQUEST['txt_username']; // here user name is John


$query  = mysql_query("SELECT txt_email,rxt_email FROM DriveRegisterTable WHERE txt_username ='$txt_username'");


//$sql = "SELECT txt_email,rxt_email FROM DriveRegisterTable WHERE txt_username ='$txt_username' ORDER BY id ";
$res = mysql_query($query);


if($query){    // Dont know how to send mail or what to write in this condition block


$to      = $txt_Mail;      // want [email protected] here only but not able to fetch this mail here
$subject = 'Hello Subject!';
$message = 'Hi';
$headers = 'From: $txt_email' . "\r\n" .
'Reply-To: txt_email' . "\r\n" .        // want [email protected] here but txt_email does not give any value here means [email protected]
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

}


mysql_close();

?>

I just want to select [email protected] and [email protected] from John and send mail.

Any Idea or suggestions would be highly welcome.

2
  • 1
    Are you really using CakePHP..? Commented May 21, 2014 at 21:24
  • 1
    If you're using CakePHP, do not use mysql_query under any circumstances. If this application is on the public internet it's vulnerable to SQL injection because of what you've done here. Commented May 21, 2014 at 21:33

2 Answers 2

2

Try this:

$txt_username = mysql_real_escape_string($txt_username);
$query = mysql_query("SELECT txt_email,rxt_email FROM DriveRegisterTable WHERE txt_username ='$txt_username'");

//use loop if you are expecting more than 1 rows, otherwise remove loop
// and just use $row = mysql_fetch_array($query);
while ($row = mysql_fetch_array($query)) {
     $email1 = $row['txt_email'];
     $email2 = $row['rxt_email'];

     //code to send email to those 2 emails
}

Note that I added mysql_real_escape_string to escape your variable. Dont just plug into your query it is vulnerable to SQL Injection.

I would suggest looking at PDO or MySQLi prepared statements.

Sign up to request clarification or add additional context in comments.

1 Comment

how to write email1 here instead of txtMail $headers = 'From: $row["txt_email"]' . "\r\n" . 'Reply-To: txt_email' . "\r\n" . This line not give me correct value
0

Youre going to loop through your query

also your $res is redundant, its better to remove that

if($query){    // Dont know how to send mail or what to write in this condition block

 while ($row = mysql_fetch_array($query)) {
   $txt_Mail = $row['txtMail'];
   $rxtMail = $row['rxtMail'];

   $to = $txt_Mail;  // want [email protected] here only but not able to fetch  
   $subject = 'Hello Subject!';
   $message = 'Hi';
   $headers = 'From:'. $rxtMail .' . "\r\n" .
  'Reply-To: '. $rxtMail .' . "\r\n" .        // want [email protected] here but txt_email  does not give any value here means [email protected]
  'X-Mailer: PHP/' . phpversion();

   mail($to, $subject, $message, $headers);
}

}

next time, try to use mysqli (Mysql Improved) or PDO. mysql functions is deprecated and for the future it wont be supported anymore

2 Comments

Yes the code is excellent this is what I want but please check these line $headers = 'From: $txt_email' . "\r\n" . 'Reply-To: txt_email' . "\r\n" . these line do notgive me from: [email protected] like but first one give me correct value to:[email protected]
on console this is showing From: $txt_email Reply-To: $txt_email

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.