PHP newbie here. Here's an example of my table in a MySQL database I'm pulling data from.
id classA classB value
------------------------------
1 A A 1
2 A B 5
3 A C 2
4 B A 1
5 B B 5
6 B C 1
7 C A 8
8 C B 5
9 C C 7
The user in puts a list of Class categories (A, B, C etc.) and my code will return the values from every combination of these pairs (e.g. [A,A], [A,B], [A,C]... etc.). I can achieve this quite easily using the following script where $array
is the input list (e.g. [A, B, C]):
<?php
// Mehtod 1 - slow
for($i = 0; $i < count($arr); $i++){
for ($j = 0; $j < count($arr); $j++){
$sql = "SELECT value FROM data_table WHERE '$arr[$i]'=classA AND '$arr[$j]'=classB LIMIT 1";
$value = mysqli_query($con,$sql);
$value = mysqli_fetch_array($corr)[0];
$results[] = array('classA' => $arr[$i], 'classB' => $arr[$j], 'value' => $value);
}
}
?>
However, this is quite slow because the mysqli_query()
is inside the for loop. Instead I'd prefer to do the query in a single call. I've tried the following with no luck..
<?php
// Mehtod 2 - fast
for($i = 0; $i < count($arr); $i++){
for ($j = 0; $j < count($arr); $j++){
// make array of class combinations
$query_array[] = array('classA' => $arr[$i], 'classB' => $arr[$j]);
}
}
// get arrays of pairs to request
$match1 = array_column($query_array, 'classA');
$match2 = array_column($query_array, 'classB');
$sql = "SELECT classA, classB, value FROM data_table WHERE classA IN '$match1' AND classB IN '$match2'";
$results = mysqli_query($con,$sql);
$results = mysqli_fetch_array($results);
?>
Can I make a query like this with a single request? I'm a bit stuck. Cheers.
$match1
and$match2
directly in your SQL query since they both are still arrays.classA
andclassB
are string columns in your DB table.Code:
Update:
You can replace
with
Since you want all the possible combinations, you do not need to build combinations inside PHP and then use them in query.
I would rather do the following:
This would consider all the combinations. It will be equivalent to:
In this case, the PHP code would look as follows:
Most Importantly, to avoid against SQL injection related attacks, you should rather use Prepared Statements