<?php
$con = mysqli_connect('localhost', 'root', 'root');
if (!$con) {die('Could not connect to databse' . mysqli_error($con));}
mysqli_select_db($con, 'timetable');
mysqli_set_charset($con, 'utf-8');
$sql = "SELECT * FROM time WHERE date = '2019-3-21'";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result);
$b=array();
for($i = 8; $i<=20; $i+=2){
if($row[$i.':00'] == 1){
array_push($b, 1);}
else{
array_push($b, 0);}
echo json_encode($b);
}
?>我期望得到echo 1,1,0,0,1,0,但实际上输出是7个数组,像11,11,1,0,01,1,0,0,0,1,1。我不知道代码出了什么问题。数据库很简单,如图所示。Thx database data
发布于 2019-03-22 10:12:55
您的循环将多次打印数组,如果您只想在循环结束时打印一次数组,则应该这样做
<?php
$con = mysqli_connect('localhost', 'root', 'root');
if (!$con) {die('Could not connect to databse' . mysqli_error($con));}
mysqli_select_db($con, 'timetable');
mysqli_set_charset($con, 'utf-8');
$sql = "SELECT * FROM time WHERE date = '2019-3-21'";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result);
$b=array();
for($i = 8; $i<=20; $i+=2){
if($row[$i.':00'] == 1){
array_push($b, 1);}
else{
array_push($b, 0);}
}
echo json_encode($b);
?>https://stackoverflow.com/questions/55291833
复制相似问题