如何从数据库中动态选择电子邮件地址?

时间:2013-09-29 12:41:52

标签: php mysql

每次将记录插入表格时,我都会使用以下PHP代码发送电子邮件。

<?php
    $latitude = $_GET['latitude'];
    $longitude = $_GET['longitude'];
    $uid = $_GET['uid'];
    $type = $_GET['type'];
    $server = "itp.db.8307610.hostedresource.com";
    $username = "abc";
    $password = "xyz";
    $database = "abc";
    $query = "INSERT INTO gps (latitude, longitude, uid, type) VALUES ($latitude, $longitude, $uid, '$type')";
    $query1 = "SELECT email FROM musers WHERE uid = $uid";       
    $result = mysqli_query($link,$query1) or die("query error".mysqli_error());
    $email = mysqli_fetch_array($result, MYSQLI_ASSOC);
    @mail("$email","New record added","A new record has been added,"From: info@xyz.com");
    $link = mysqli_connect($server, $username, $password) or die ("error connecting to database");
    mysqli_select_db($database, $link);
    mysqli_query($query, $link) or die("query error");
    mysqli_query($query1, $link) or die("query error");
    mysqli_close($link);
    echo "SUCCESS\n";
?>

这没有问题。我已经对电子邮件ID进行了硬编码 如何根据以下查询从musers表动态添加电子邮件。

select email from musers where uid = $uid;

编辑:我在进行更改后收到以下错误消息。

  

警告:mysqli_query()要求参数1为mysqli,在第12行的/home/content/i/t/p/itp/html/data/email.php中给出为null

     

警告:mysqli_error()预计在第12行的/home/content/i/t/p/itp/html/data/email.php中给出了1个参数0   查询错误

2 个答案:

答案 0 :(得分:1)

$result = mysqli_query($link,$query1) or die("query error".mysqli_error());
$email = mysqli_fetch_array($result, MYSQLI_ASSOC);
mail($email['email'],"you@yourdomain.com","message");

答案 1 :(得分:1)

有很多问题:

1°您正在尝试在连接数据库之前执行查询。

2°你没有正确地关闭你的琴弦......

尝试此修复:

<?php
    $latitude = $_GET['latitude'];
    $longitude = $_GET['longitude'];
    $uid = $_GET['uid'];
    $type = $_GET['type'];
    $server = "itp.db.8307610.hostedresource.com";
    $username = "abc";
    $password = "xyz";
    $database = "abc";
    $link = mysqli_connect($server, $username, $password) or die ("error connecting to database");
    mysqli_select_db($database, $link);
    $query = "INSERT INTO gps (latitude, longitude, uid, type) VALUES ($latitude, $longitude, $uid, '$type')";
    $query1 = "SELECT email FROM musers WHERE uid = $uid";       
    $insert = mysqli_query($link,$query) or die("query error".mysqli_error());
    $result = mysqli_query($link,$query1) or die("query error".mysqli_error());
    $email = mysqli_fetch_assoc($result);
    @mail($email['email'],"New record added","A new record has been added","From: info@xyz.com");
    mysqli_close($link);
    echo "SUCCESS\n";
?>

但是,正如一些人已经提到的那样:准备你的陈述或逃避你的意见......