Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将2个mysql查询合并为一个_Php_Mysql - Fatal编程技术网

Php 将2个mysql查询合并为一个

Php 将2个mysql查询合并为一个,php,mysql,Php,Mysql,我有一个博客。每次用户评论帖子时,他们都可以选择“通过电子邮件通知我新评论”。但是我在向选中复选框的用户发送邮件时遇到了一个小问题 用户每次发表评论时都应该得到通知,并选中复选框“通知我…”以获取新评论 insert_comments表单正在将数据发送到add_comment.php,我已经在add_comment.php中输入了发送电子邮件的代码 在mysql插入注释查询之后。我有两个疑问: SELECT MAX(id) AS last_id FROM `comments` WHERE pos

我有一个博客。每次用户评论帖子时,他们都可以选择“通过电子邮件通知我新评论”。但是我在向选中复选框的用户发送邮件时遇到了一个小问题

用户每次发表评论时都应该得到通知,并选中复选框“通知我…”以获取新评论

insert_comments表单正在将数据发送到add_comment.php,我已经在add_comment.php中输入了发送电子邮件的代码

在mysql插入注释查询之后。我有两个疑问:

SELECT MAX(id) AS last_id FROM `comments` WHERE post_id = $post_id
SELECT id AS user_who_wants_to_get_notified, email FROM `comments` WHERE notify = 'yes' AND post_id = $post_id
但我不知道如何将它们组合成一个查询

add_comment.php如下所示:

<?php

include_once "blog_functions/config.php";

include_once "blog_functions/func/blog.php";

$name = $_POST['name'];
$comment = $_POST['comment'];
$email = $_POST['email'];
$post_id = $_POST['post_id'];

if(isset($_POST['notify_me'])){
    $notify_me = $_POST['notify_me'];
} else {
    $notify_me = "No";
}

$time = time();
$actual_time = date('d-m-Y H:i:s', $time);  

 if(!empty($name && $comment && $post_id)){

     if(isset($_POST['email']) && filter_var($email, FILTER_VALIDATE_EMAIL)){
        mysql_query("INSERT INTO comments(post_id, name, email, comment, notify, date) VALUES('$post_id', '$name', '$email', '$comment', '$notify_me', '$actual_time')");

        $query = mysql_query("(SELECT MAX(id) AS last_id FROM `comments` WHERE post_id = $post_id) UNION (SELECT id AS user_who_wants_to_get_notified, email FROM `comments` WHERE notify = 'yes' AND post_id = $post_id)"); 
        $row = mysql_fetch_assoc($query);

        if(row['last_id'] > row['user_who_wants_to_get_notified']){

            mail($row['email'], "New comments on a post you have commented", "There has recently been commented on a post which you want to receive notifications on. \n\n The News Blog", "From: Rabeea96@hotmail.com");
        }

        header('location: blog.php?id=' . $post_id);

     } else {
         echo "There was a problem sending the email, please try again later";
     }

} else {

    header('location: blog.php');
}

真正的问题是,为什么您需要两个查询?如果请求订阅电子邮件通知的用户的请求与插入注释的请求相同,那么您已经拥有注释id,根本不需要查询数据库。当您插入
时,您已经这样做了。有关详细信息,请参阅

此外,依赖于像
MAX(id)
这样的东西是一个非常糟糕的主意,因为不能保证它会为您提供刚刚插入一行的id。那时可能已经发布了另一条评论,或者最大的id不一定是最后一条(例如,两个请求同时出现,而您正在查找的恰好是倒数第二个)


是的,如果您愿意,您可以运行多个查询,但在这里您确实不想这样做,因为这对您没有任何好处。您只需要从上一次查询中获取
insert\u id
,并使用它在单独的查询中查找订户。

mysqli支持多查询。1) 注释中可能不应该包含电子邮件(它应该来自连接上的users表。2)max()可能不太好。某种类型的记录表或诸如此类的东西。因此,如果有3个或4个查询需要处理,那么不要跳过其中的一些,而去查找max(),这两个查询有何关联?看起来你并没有把一个的结果用在另一个上。从目前的情况来看,它很可能主要是基于观点的——有很多方法可以做到这一点。我在上面的描述中插入了add_comment.php的代码。当我想向用户发送电子邮件时,我使用两个查询的结果。我在上面的描述中插入了add_comment.php的代码。如果你看一下代码,你就会知道我为什么需要这些查询。不,对不起。我真的不知道。我所看到的只是一段代码,它确切地说明了为什么您不需要这些查询,并支持为什么我的答案更有效、更高效、更黄道:)