如何从sql数据库表中获取列数据并将该数据放入PHP中的字符串中?

问题描述:

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 " johnery99@gmail.com" and rxtMail is "sarah88@gmail.com"

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 johnery99@gmail.com and sarah88@gmail.com 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 johnery99@gmail.com here only but not able to fetch this mail here
$subject = 'Hello Subject!';
$message = 'Hi';
$headers = 'From: $txt_email' . "
" .
'Reply-To: txt_email' . "
" .        // want sarah88@gmail.com here but txt_email does not give any value here means sarah88@gmail.com
'X-Mailer: PHP/' . phpversion();

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

}


mysql_close();

?>

I just want to select johnery99@gmail.com and sarah88@gmail.com from John and send mail.

Any Idea or suggestions would be highly welcome.

我正在尝试很多,但对我来说没什么用。 p>

我的查询 是如何从sql数据库表中获取列数据并将该数据放入PHP中的字符串? p>

请检查我在做什么。 p>

我有“DriveRegisterTable”表,其中数据库表中有4列,第一个“id”第二个“用户名”第三个“txtMail”第四个“rxtMail”。 p>

现在我想选择txtMail和 来自数据库表的rxtMail,其中username =“John” p>

现在在我的数据库表“username”中,john有txtMail是“johnery99@gmail.com”,rxtMail是“sarah88@gmail.com” p>

现在我想在我的php字符串上获取这些东西。 p>

现在我想从数据库表中选择txtMail和rxtMail,其中username =“John “并在PHP字符串中输入johnery99@gmail.com和sarah88@gmail.com的txtMail和rxtMail值,然后发送邮件。 p>

请检查我在做什么它不起作用 对我来说: p>

 &lt;?php 
 
 
 $ db_host =  “localhost”; 
 $ username =“jacktask”; 
 $ db_pass =“sormor4455”; 
 $ database =“myDatabaseDB”; 
 
mysql_connect(“$ db_host”,“$ username”,“$ db_pass”  ); 
 
 @ mysql_select_db($ database)或die(“无法找到database4”); 
 
 $ txt_username = $ _REQUEST ['txt_username'];  //这里的用户名是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){//不知道如何发送邮件或写入的内容 这个条件块
 
 
 $ n = $ txt_Mail;  //这里只想要johnery99@gmail.com但是不能在这里获取此邮件
 $ subject ='Hello Subject!'; 
 $ message ='嗨'; 
 $ headers ='From:$ txt_email'。  “
 
”。
'Reply-To:txt_email'。  “
 
”。  //想要sarah88@gmail.com这里但是txt_email在这里没有给出任何值意味着sarah88@gmail.com 
'X-Mailer:PHP /'。  phpversion(); 
 
mail($ to,$ subject,$ message,$ headers); 
 
} 
 
 
mysql_close(); 
 
?&gt; 
  code>  
 
 

我只想从John选择johnery99@gmail.com和sarah88@gmail.com并发送邮件。 p>

任何想法或建议都会很高 欢迎。 p> div>

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 johnery99@gmail.com here only but not able to fetch  
   $subject = 'Hello Subject!';
   $message = 'Hi';
   $headers = 'From:'. $rxtMail .' . "
" .
  'Reply-To: '. $rxtMail .' . "
" .        // want sarah88@gmail.com here but txt_email  does not give any value here means sarah88@gmail.com
  '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

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.