1

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.

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);
3
  • This two queries doing the same why u cant use it in one query ? Commented Jul 11, 2016 at 10:24
  • Because I am querying two tables, and this will increase to hundrededs over time Commented Jul 11, 2016 at 10:24
  • 1
    Try with dev.mysql.com/doc/refman/5.7/en/union.html Commented Jul 11, 2016 at 10:26

1 Answer 1

1

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'];
}
Sign up to request clarification or add additional context in comments.

3 Comments

Still doesn't work, only getting one of the two values
Is not-set is present in second table column value??
My error, database not re-written correctly, works now, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.