我陷入了一个任务,只会在两个数组之间携带一个for-loop
来计算数组并选择查询来获取结果。我只得到第一个匹配的结果而不是下一个记录。我是什么意思做的是
<?php
//db cnnect
//fetch between 06:00:00 to 08:30:00 09:00:00 to 10:00:00
$date=array('06:00:00','09:00:00');
$date1=array('08:30:00','10:00:00');
$count=count($date);
for($i=0;$i<$count;$i++){
$sql="SELECT count(*) AS test FROM fo WHERE DATE_FORMAT(sys_time,'%H:%m:%i') BETWEEN DATE_FORMAT(sys_time,'$date[$i]') AND DATE_FORMAT(sys_time,'$date1[$i]')";
$i=$i+1;
}
$query=mysql_query($sql);
if(!$query){
die('could not connect'.mysql_error());
}
echo'<html>
<head>
<title>Count User Info TimeWise</title>
</head>
<h1>Count User</h1>
<table border="3" cellspacing="2">
<tr>
<th>Count</th>';
while($row=mysql_fetch_array($query)) {
echo "<tr>";
$str=$row['test'];
$subcategory = explode(',', $str);
foreach($subcategory as $value) {
echo "<td>" . $value . "</td>";
}}
echo "</table>";
echo "</html>";
?>
它正好在6-8之间取得第一条记录而不是9-10之间的第一条记录。我做错了什么。我怎么能得到它。谢谢
答案 0 :(得分:3)
很确定你有额外的增量。 此外,如果你想运行2个单独的查询,你的大括号在错误的位置。 您还应该修复您的html以使其有效。
<?php
//db cnnect
//fetch between 06:00:00 to 08:30:00 09:00:00 to 10:00:00
$date=array('06:00:00','09:00:00');
$date1=array('08:30:00','10:00:00');
echo'<html>
<head>
<title>Count User Info TimeWise</title>
</head>
<body>
<table>
<tr>
<th>Count</th>
</tr>';
$count=count($date);
for($i=0;$i<$count;$i++){
$sql="SELECT count(*) AS test FROM fo WHERE DATE_FORMAT(sys_time,'%H:%m:%i') BETWEEN DATE_FORMAT(sys_time,'$date[$i]') AND DATE_FORMAT(sys_time,'$date1[$i]')";
//This is unneccessary since you increment it in your for loop
//$i=$i+1;
//Moving this down so it prints both query results
//}
$query=mysql_query($sql);
if(!$query){
die('could not connect'.mysql_error());
}
while($row=mysql_fetch_array($query))
{
echo "<tr>";
$str=$row['test'];
$subcategory = explode(',', $str);
foreach($subcategory as $value)
{
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
//New }
}
echo "</table>";
echo "</html>";
?>
您还应该考虑升级到PDO,因为mysql扩展在某些方面被认为是不安全的,不再受支持。
答案 1 :(得分:1)
$count=count($date);
for($i=0; $i < $count; $i++) {
$sql="SELECT count(*) AS test FROM fo WHERE DATE_FORMAT(sys_time,'%H:%m:%i') BETWEEN DATE_FORMAT(sys_time,'$date[$i]') AND DATE_FORMAT(sys_time,'$date1[$i]')";
// $i = $i + 1; delete this line
// rest of your handling code
$query=mysql_query($sql);
if(!$query){
die('could not connect'.mysql_error());
}
echo'<html>
<head>
<title>Count User Info TimeWise</title>
</head>
<body>
<table>
<tr><th>Count</th></tr>';
while($row=mysql_fetch_array($query)) {
echo "<tr>";
$subcategory = explode(',', $row['test']);
foreach($subcategory as $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
echo "</table>";
echo "</html>";
}
答案 2 :(得分:1)
你的结构应该是这样的:
<?php
echo'<html>
<head>
<title>Count User Info TimeWise</title>
</head>
<body>
<table>
<tr>
<th>Count</th>
</tr>';
//db cnnect
//fetch between 06:00:00 to 08:30:00 09:00:00 to 10:00:00
$date=array('06:00:00','09:00:00');
$date1=array('08:30:00','10:00:00');
$count=count($date);
for($i=0;$i<$count;$i++){
$sql="SELECT count(*) AS test FROM fo WHERE DATE_FORMAT(sys_time,'%H:%m:%i') BETWEEN DATE_FORMAT(sys_time,'$date[$i]') AND DATE_FORMAT(sys_time,'$date1[$i]')";
$query=mysql_query($sql);
if(!$query){
die('could not connect'.mysql_error());
}
while($row=mysql_fetch_array($query)) {
echo "<tr>";
$str=$row['test'];
$subcategory = explode(',', $str);
foreach($subcategory as $value) {
echo "<td>" . $value . "</td>";
}
echo '</tr>';
}
}
echo "</table></body>";
echo "</html>";
?>
注意: mysql_*
已被弃用mysqli_*
而不是