按我设定的循环工作仅一次,它应该工作两次或更多次

时间:2019-07-28 17:41:54

标签: php loops

我创建了一个循环并将值存储在数组中,然后foreach发送邮件将其抛出,但是它只能工作一次,在那之后不起作用,并且我还尝试了如果未在邮件中搜索电子邮件ID的情况就不会进行,但不会找到任何错误,但代码也无法正常工作

我也尝试了太多代码,但没有用

// run query
$query = mysqli_fetch_assoc(mysqli_query($conn,"SELECT email FROM login where  id='248' OR id='241' OR id='247' "));

// set array
$array = array();


    // add each row returned into an array
    $array[] = $query['email']; 


    foreach ($array as $item) {

 $user['email'] = $item;

    $search_history = mysqli_fetch_assoc(mysqli_query($conn,"select user_id,search_name from search_history HAVING  user_id = '".$user['email']."' ORDER BY id DESC limit 5"));
        $product_keyword = $search_history['search_name'] ."Products";
        $product_keyword1 = $search_history['search_name'];
        $user_id1 = $search_history['user_id'];

        $products = '<table align="left" class="em_wrapper" width="560" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                    <td valign="top" align="center">';

        $qry="select * from add_new_product WHERE keyword LIKE '%$product_keyword1%' AND status=1 ORDER BY pro_id DESC LIMIT 12 ";
        $res=mysqli_query($conn,$qry);

1 个答案:

答案 0 :(得分:-2)

摘要: 通过阅读代码,您尝试从登录表中获取与ID号为241 247 248匹配的电子邮件字段

由于您需要将email列作为数组,以便执行针对“ search_history”表的辅助SQL搜索。

一个更简单的方法是执行SQL连接 https://www.w3schools.com/sql/sql_join.asp

然后将返回所有结果,其中login.id =电子邮件

这将使您腾出时间遍历结果并执行第3条SQL语句

我注意到您的代码中定义了以下3个变量,但从未在您提供的代码段中使用

$ products

$ product_keyword

$ user_id1

<?PHP
    echo "Stackoverflow Question ID: 57243466\n";
    echo "<hr>";
    $Username = "root";
    $Password = "":
    $Host = "localhost";
    $databaseName = "my_database";

    try{
        $con = new PDO("mysql:host=" . $Host . ";dbname=" . $databaseName, $Username, $Password]);
        $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $sql = "SELECT history.search_name, history.userID, login.email FROM login RIGHT JOIN search_history AS history ON history.userID=login.email WHERE login.id='248' OR login.id='241' or login.id='247'";
        $stmt = $con->prepare($sql); 
        $stmt->execute();

        foreach($stmt as $item){

            //Please note you have not made any use of the following variable declarations in your code uncomment the lines beneath if you do need to use them.
            //$product_keyword = $item['search_name'] ."Products";
            //$user_id1 = $item['user_id'];

            $products = '<table align="left" class="em_wrapper" width="560" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top" align="center">';       

            $sql = "SELECT * FROM add_new_product WHERE keyword LIKE '%:product_keyword1%' AND status=1 ORDER BY pro_id DESC LIMIT 12 ";
            $res = $con->prepare($sql);
            $res->execute(["product_keyword1"=>$item['search_name']);

            foreach($res as $i){
                // do something with the SQL Query you have performed
            }
    }
}catch(exception $e){
    echo $e->getMessage();
}
?>