I'm trying to make a new column with results from an SQL query in PHP:
$someArray= array(array('match'=>'123'), array('match'=>'456'), array('match'=>'789')); //arbitrary number of elements
foreach($someArray as $key=>$item){
$someArraysDouble[]=$item;
}
$someQuery="select count(*) as somecount from sometable";
$probe1=array();
$probe2="0";
$probe3="0";
$probe4="0";
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";//myDB uses MySQL
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
foreach($someArray as $key=>$item) {
$someQuery.=" where somecolumn like "%$item['match']%";
$blahblah=$conn->query($someQuery);
if ($blahblah->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$row['match']=$item['match'];
$probe1[]=$row;
}
}
$conn->close();
}
foreach($someArraysDouble as $key1=>$item1) {
foreach($probe1 as $key2=>$item2) {
if($item2['match']==$item1['match']) {
$probe2=$item1['somecount'];
$probe3=$item2['somecount'];
$item1['somecount']=$item2['somecount'];
$probe4=$item1['somecount'];
}
}
}
The output HTML looks like this:
<html>
<head></head>
<body>
{$probe2}<br>{$probe3}<br>{$probe4}<br><br>
{loop $probe1 $key1 $item1}
{$item1['somecount']}<br><br>
{/loop}
<br><br>
{loop $someArraysDouble $key2 $item2}
{$item2['somecount']}<br><br>
{/loop}
</body>
</html>
Result is... something I don't understand:
- $probe2 is null, which is expected.
- $probe3 is the count value for last element, which is expected.
- $probe4 is the count value for last element, which is expected.
- The first loop with $probe1 produces the count value for each element, which is expected.
- The second loop with $someArraysDouble produces nothing, which is NOT expected. HOW can this happen?
For some reason that I'm not sharing in order to keep this question concise, I need to have the count value for each element outputted via $someArraysDouble.
$someQuery.=" where somecolumn like "%$item['match']%";The number/type of brackets do not match. try:$someQuery.=" where somecolumn like '%$item['match']%' ";$someArraygenerated..?