I need to query db to get info, also inside the foreach I need to make some more queries to select and count rows (just checks). I think I can't join queries if I need to count rows...
So I have this:
// first query here
$Items = $mysqli->query("SELECT * ...");
foreach ($Items as $i => $ItemInfo) {
// some checks here before start printing db data
$a = $mysqli->query("SELECT * ...");
$num_a = mysqli_num_rows($a); // 61
$b = $mysqli->query("SELECT * ...");
$num_b = mysqli_num_rows($b); // 63
}
I'm getting this:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\views\siteeto.php on line 61
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\views\siteeto.php on line 63
I tried some examples from comments in php.net manuals but not working:
<?php
// WORKING CODE:
$mysqli->multi_query(" Many SQL queries ; "); // OK
while ($mysqli->next_result()) // flush multi_queries
{
if (!$mysqli->more_results()) break;
}
$mysqli->query(" SQL statement #1 ; ") // now executed!
$mysqli->query(" SQL statement #2 ; ") // now executed!
$mysqli->query(" SQL statement #3 ; ") // now executed!
$mysqli->query(" SQL statement #4 ; ") // now executed!
?>
Maybe I'm doing it wrong:
// multi_query here
$Items = $mysqli->multi_query("SELECT * ..."); // doing a normal query???
while ($mysqli->next_result()) // flush multi_queries {
if (!$mysqli->more_results()) break;
}
foreach ($Items as $i => $ItemInfo) {
// some checks here before start printing db data
$a = $mysqli->query("SELECT * ...");
$num_a = mysqli_num_rows($a);
$b = $mysqli->query("SELECT * ...");
$num_b = mysqli_num_rows($b);
}
And I'm getting this:
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\views\siteeto.php on line 50
Maybe I don't need multi_query because I don't need any extra query there...
Anyways, I don't care using prepared statements, probably I'll use them when I get everyting working fine. But the problem is I need the first query (outside the foreach) and I can't close it because I'm printing data in the foreach and I need those checks (row counts) in the foreach also.
Thanks in advance!
Edit: Add queries:
if (login_check($mysqli) == true) {
$id = $_SESSION['user_id'];
}
else {
$id = get_ip_address();
}
$status_id = 1; // status message id
if ($stmt = $mysqli->prepare(" (SELECT * FROM user_uploads ORDER by up_time DESC)
UNION
(SELECT COUNT(*) rowCount FROM likes WHERE user_id = ? AND status_id = ? AND img_id = ?)
UNION
(SELECT COUNT(*) rowCount2 FROM likes WHERE status_id = ?)")) {
$stmt->bind_param('iii', $id, $status_id, $status_id, $img_id);
$Items = $stmt->execute(); // get photos
}
foreach ($Items as $ItemInfo) {
//how to get the rowCount???
}
First fetch all your data into an array (http://php.net/manual/tr/mysqli-result.fetch-all.php), then iterate over it.
$query = $mysqli->query("Your sql");
$data = $mysqli->fetchall($query);
foreach ($data as $item) {
// more queries here
}
Or even better: Create a join query to fetch all data at once!
reference for how joins work: http://www.w3schools.com/sql/sql_join.asp
For row counts in joins use GROUP BY
SELECT m.*, COUNT(s.id) as rowsInSubTable --Where id = same value as mainTable column: sId (see ON)
FROM mainTable m
LEFT JOIN subTable s
ON m.sId = s.id
GROUP BY m.id