从数据库sql查询中检索数据

时间:2016-04-19 12:34:29

标签: php sql mysqli

我想从db获取数据。 vName vName=ADMIN_EMAIL的列,此行包含名称为 vValue 的列。还有一个存储电子邮件地址的列,我希望获得该值并将其存储在$toEmail中。

请有人指导我。

这是我的 php代码

   $sqldata=mysqli_query($obj,"SELECT * FROM configuration WHERE     vName='ADMIN_EMAIL'");
   $rowcount=mysqli_num_rows($sqldata);
   // $toEmail = "dev.waqas@designbee.us";
   $mailHeaders = "From: " . $_POST["userName"] . "<". $_POST["userEmail"] .">\r\n";
   if(mail($toEmail, $_POST["subject"], $_POST["content"], $mailHeaders)) {
       echo "<p class='success'>Mail Sent.</p>";
    } else {
        echo "<p class='Error'>".$toEmail.".</p>";
    }

4 个答案:

答案 0 :(得分:1)

您需要使用mysql_fetch_assoc()来获取查询结果。 mysql_num_rows()为您提供查询结果中存在的行数。 检查以下代码。

 $sqldata=mysqli_query($obj,"SELECT * FROM configuration WHERE vName='ADMIN_EMAIL'");
 $rowcount=mysqli_num_rows($sqldata);
 if($rowcount > 0)
 {
     $result = mysqli_fetch_assoc($sqldata);
     $toEmail = $result['email']; // Your table column value
     $mailHeaders = "From: " . $_POST["userName"] . "<". $_POST["userEmail"] .">\r\n";
     if(mail($toEmail, $_POST["subject"], $_POST["content"], $mailHeaders)) {
         echo "<p class='success'>Mail Sent.</p>";
     } else {
         echo "<p class='Error'>".$toEmail.".</p>";
     }
  }

答案 1 :(得分:1)

你只需要从db中获取记录,你还没有这样做:

 $sqldata=mysqli_query($obj,"SELECT * FROM configuration WHERE vName='ADMIN_EMAIL'");
       $rowcount=mysqli_num_rows($sqldata);
    if($rowcount > 0)
    {
       $result = mysqli_fetch_assoc($sqldata); // fetch records
       $toEmail = $result['vemail']; // Your column name
       $mailHeaders = "From: " . $_POST["userName"] . "<". $_POST["userEmail"] .">\r\n";
       if(mail($toEmail, $_POST["subject"], $_POST["content"], $mailHeaders)) {
           echo "<p class='success'>Mail Sent.</p>";
        } else {
            echo "<p class='Error'>".$toEmail.".</p>";
        }
  }

答案 2 :(得分:0)

执行查询后需要获取数据。

$row = $sqldata->fetch_array(MYSQLI_ASSOC);
$toEmail= $row['vName']

答案 3 :(得分:0)

最好使用PDO,

try{
      $con=new PDO("mysql::host=localhost;dbname=databasename;","username","password");
      $query="SELECT * FROM configuration WHERE vName='ADMIN_EMAIL";
      $res=$con->query($query);
      $rows=$res->fetchAll(PDO::FETCH_BOTH);
      $email="";
      foreach($rows as $row)
      {
         $email.=$row["useremail"];//you get the user email in the var $email
      }          
//do what you want ....
}
catch(PDOException $e){
    die("error:".$e->getMessage() );
}