我的第一个代码就是这个
$it= mysql_query("SELECT comp_specs.type FROM comp_specs INNER JOIN users ON comp_specs.id=users.specification_id WHERE users.workgroup_id=1 AND comp_specs.type=0", $connection);
$it_num =mysql_num_rows($it);
$arrastre= mysql_query("SELECT comp_specs.type FROM comp_specs INNER JOIN users ON comp_specs.id=users.specification_id WHERE users.workgroup_id=2 AND comp_specs.type=0", $connection);
$arrastre_num =mysql_num_rows($arrastre);
$clinic= mysql_query("SELECT comp_specs.type FROM comp_specs INNER JOIN users ON comp_specs.id=users.specification_id WHERE users.workgroup_id=3 AND comp_specs.type=0", $connection);
$clinic_num =mysql_num_rows($clinic);
$Admin= mysql_query("SELECT comp_specs.type FROM comp_specs INNER JOIN users ON comp_specs.id=users.specification_id WHERE users.workgroup_id=4 AND comp_specs.type=0", $connection);
$Admin_num =mysql_num_rows($Admin);
这项工作很棒,但它花了很多行代码,所以我试着这样做
$field=array('$it', '$arrastre', '$clinic', '$admin' );
foreach($field as $fields){
$(fields)= mysql_query("SELECT comp_specs.type FROM comp_specs INNER JOIN users ON comp_specs.id=users.specification_id WHERE users.workgroup_id=1 AND comp_specs.type=0", $connection);
$(fields)_num =mysql_num_rows($fields);
}
但是发生了错误。
我需要一些关于如何使用foreach循环多次查询的帮助
答案 0 :(得分:0)
尝试从$(fields)
删除括号到$fields
并停止使用mysql_*
,因为它在PHP 7中不再存在
根据PHP的版本,有两个或三个PHP 用于访问MySQL数据库的API。 PHP 5用户可以选择 不推荐使用的mysql扩展,mysqli或PDO_MySQL。 PHP 7删除 mysql扩展,只留下后两个选项。
答案 1 :(得分:-1)
用于此程序
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();