需要从数据库用javascript显示数据

需要从数据库用javascript显示数据,javascript,php,ajax,for-loop,foreach,Javascript,Php,Ajax,For Loop,Foreach,我正在使用一个php代码,从输入中获取变量,并使用ajax 代码: 我在success函数中得到一个数组: object(mysqli_result)#3 (5) { ["current_field"]=> int(0) ["field_count"]=> int(6) ["lengths"]=> NULL ["num_rows"]=> int(76) ["type"]=> int(0) } 如果我使

我正在使用一个php代码,从输入中获取变量,并使用ajax

代码:

我在success函数中得到一个数组:

 object(mysqli_result)#3 (5) {
   ["current_field"]=>
   int(0)
   ["field_count"]=>
   int(6)
   ["lengths"]=>
   NULL
   ["num_rows"]=>
   int(76)
   ["type"]=>
   int(0)
 }
如果我使用php foreach,它会显示所有行,但我尝试了javascript data.foreach,它说它不存在。我还尝试了数据的forvar元素,但它将每个字符显示为一个单独的字符

如何用javascript显示查询结果中的每一行

这是模型的代码:

 $sql = "SELECT medical_agenda_id, day, `date`, time_start, time_end, hour_status_id FROM medical_agenda WHERE hour_status_id>='0' AND professional_id='".$id."' ;";
 var_dump($sql);
 $res = $this->db->query($sql);
 return $res;
我需要将结果放入success函数

return$res;将返回大量不需要的数据。相反,将结果集的数据放入关联数组中,并对其进行echo json_编码

数据类型最好是JSON而不是html。因为,这将使您获得负载中的最小数据量。你可以决定如何在前端展示它们

应该是这样的

while($row = mysqli_fetch_assoc($res)){//Using procedural style here
    $data[] = $row;
}

echo json_encode($data);
另外,var_dump$sql也应该去掉。否则,这也将包含在HTTP响应中,这将使JSON字符串无效

所有的垃圾都倒了

$sql = "SELECT medical_agenda_id, day, `date`, time_start, time_end, hour_status_id FROM medical_agenda WHERE hour_status_id>='0' AND professional_id='".$id."' ;";

//var_dump($sql); Take this out!

$res = $this->db->query($sql);

//return $res; Take this out! You will return the data by echoing it to the Web Server

while($row = mysqli_fetch_assoc($res)){//Using procedural style here
    $data[] = $row;
}

echo json_encode($data); //This is the output tho the client's request.
我们应该做到这一点


这是使代码正常工作所能做的最低限度。在投入生产之前,您需要进行许多优化。

在返回结果之前,您需要在PHP中获取结果。它在控制台上为我提供了完美的数据,我现在需要它来生成与用户交互的链接。这样他就可以看到一个表,其中包含数据,并按照特定位置的信息进行排序。我不明白是否必须在javascript中使用data[date]或data.date
$sql = "SELECT medical_agenda_id, day, `date`, time_start, time_end, hour_status_id FROM medical_agenda WHERE hour_status_id>='0' AND professional_id='".$id."' ;";

//var_dump($sql); Take this out!

$res = $this->db->query($sql);

//return $res; Take this out! You will return the data by echoing it to the Web Server

while($row = mysqli_fetch_assoc($res)){//Using procedural style here
    $data[] = $row;
}

echo json_encode($data); //This is the output tho the client's request.