使用两个不同的foreach循环将相同的数组添加到同一数组中PHP/SQL [英] Adding to the same array with two different foreach loops PHP/SQL

查看:32
本文介绍了使用两个不同的foreach循环将相同的数组添加到同一数组中PHP/SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单独的表,里面装满了数据,并且每个表都需要相同的数据.例如,我要选择的第一个表的值为3623,第二个表的值为3852.

I have seperate tables full of data and I require the same data from each table. For example the first table I am selecting from has the value 3623 and the second table has the value 3852.

我正在尝试将这两个值都放入一个数组中,然后在下一行绘制在图形上.我正在使用的代码可以在下面看到,问题是第一个foreach循环的值被添加了,而不是第二个.所以我最终只有3623,而不是3852,这是一个问题.

I am trying to get both of these values into an array to then be plotted on a graph later down the line. The code I am using can be seen below, the issue is that on the value from the first foreach loop gets added and not the second one. so I end up with just 3623 and not the 3852 as well which is an issue.

$datay1 = array();
$yes = "not-set";
$sql = "SELECT * FROM `0530-0605` WHERE SearchTerm = :yes";
$stmt = $conn->prepare($sql);
$stmt->bindParam(":yes", $yes);
$stmt->execute();
foreach($stmt as $row) {
    $datay1[] = $row['Clicks'];
}

$sql = "SELECT * FROM `0606-0612` WHERE SearchTerm = :yes";
$stmt = $conn->prepare($sql);
$stmt->bindParam(":yes", $yes);
$stmt->execute();
foreach($stmt as $row) {
    $datay1[] = $row['Clicks'];
}

print_r($datay1);

推荐答案

您可以使用

You can use UNION ALL to merge result of two query as

$sql = "SELECT * FROM `0530-0605` WHERE SearchTerm = :yes
UNION ALL
SELECT * FROM `0606-0612` WHERE SearchTerm = :yes1";

$stmt = $conn->prepare($sql);
$stmt->bindParam(":yes", $yes);
$stmt->bindParam(":yes1", $yes);
$stmt->execute();
foreach($stmt as $row) {
    $datay1[] = $row['Clicks'];
}

这篇关于使用两个不同的foreach循环将相同的数组添加到同一数组中PHP/SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆