查询foreach num_rows内部和外部的查询

时间:2014-10-22 12:38:18

标签: php mysqli

我需要查询db来获取信息,同样在foreach中我需要做一些更多的查询来选择和计算行(只是检查)。如果我需要计算行数,我想我不能加入查询......

所以我有这个:

// 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
}

我得到了这个:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\views\site\reto.php on line 61

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\views\site\reto.php on line 63

我在php.net手册中的注释中尝试了一些示例,但没有工作:

<?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!
?>

也许我做错了:

// 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);
}

我得到了这个:

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\views\site\reto.php on line 50

也许我不需要multi_query因为我不需要任何额外的查询...

无论如何,我不在乎使用准备好的陈述,可能我会在每个工作正常时使用它们。但问题是我需要第一个查询(在foreach之外)并且我无法关闭它因为我在foreach中打印数据而且我还需要foreach中的那些检查(行计数)。

提前致谢!

编辑:添加查询:

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???
}

1 个答案:

答案 0 :(得分:0)

首先将所有数据提取到一个数组(http://php.net/manual/tr/mysqli-result.fetch-all.php)中,然后迭代它。

$query = $mysqli->query("Your sql");
$data =  $mysqli->fetchall($query);
foreach ($data as $item) { 
   // more queries here 
}

甚至更好:创建连接查询以立即获取所有数据!

关于联接如何工作的参考:http://www.w3schools.com/sql/sql_join.asp

示例加入查询

对于连接中的行计数,请使用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

分组依据:http://www.w3schools.com/sql/sql_groupby.asp