查询中的 WHERE 子句在 CodeIgniter 3 中无法正常工作
WHERE clause in the query doesn't work properly in CodeIgniter 3
我正在为一个项目使用 CodeIgniter 3,并在控制器中编写了以下函数以将一些数据传递给视图。它查询数据库中的 table 以获取血型作为标签,并获取“isAvailable = 1”处每种血型的行数。然后将这些数据传递到视图中以呈现图表。但如您所见,这些计数是错误的。即使“isAvailable = 0”,它也会对行进行计数。我的代码有什么问题,我该如何解决?
控制器中的函数。
public function bloodTypesChart()
{
$chartData = [];
$blood_types = $this->db->query("SELECT (BloodType) as blood_type FROM packets WHERE (isAvailable) = '1' GROUP BY blood_type")->result_array();
foreach($blood_types as $bt)
{
$record = $this->db->query("SELECT COUNT(PacketID) as count FROM packets WHERE BloodType = '{$bt['blood_type']}'")->result_array();
foreach($record as $row) {
$chartData['label'][] = $bt['blood_type'];
$chartData['data'][] = $row['count'];
}
}
$chartData['chart_data'] = json_encode($chartData);
$this->load->view('insight',$chartData);
}
查看
<script>
new Chart(document.getElementById("bar-chart"), {
type: 'bar',
data: {
labels: <?= json_encode($label)?>,
datasets: [
{
label: "Donations",
backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
data: <?= json_encode($data)?>
}
]
},
options: {
legend: { display: false },
title: {
display: true,
text: 'Donations'
}
}
});
</script>
在您的查询中,因为您在括号中封装了 only 字段名称,WHERE (isAvailable) = '1'
的计算结果为 WHERE there is a field labeled isAvailable
- 对于table。删除括号,它应该可以正常工作
$blood_types = $this->db->query("SELECT (BloodType) as blood_type FROM packets WHERE isAvailable = '1' GROUP BY blood_type")->result_array();
你这个方法太复杂了,避免使用php循环获取你可以通过简单查询获取的数据。
只需使用此 mysql 查询,计算 isAvailable 行数,当为真 (1) 时:
$sql=" SELECT BloodType as blood_type, COUNT(PacketID) as mycount
FROM packets
WHERE isAvailable = 1
GROUP BY blood_type
";
注意:我已将别名 count 更改为 mycount,因为 count 是保留字。
你的函数应该是这样的:
public function bloodTypesChart()
{
$sql=" SELECT BloodType as blood_type, COUNT(PacketID) as mycount
FROM packets
WHERE isAvailable = 1
GROUP BY blood_type
";
$chartData = [];
$blood_types = $this->db->query($sql)->result_array();
foreach($blood_types as $row)
{
$chartData['label'][] = $row['blood_type'];
$chartData['data'][] = $row['mycount'];
}
$chartData['chart_data'] = json_encode($chartData);
$this->load->view('insight',$chartData);
}
这里 sql-fiddle 使用您的数据库的简化版本执行查询
我正在为一个项目使用 CodeIgniter 3,并在控制器中编写了以下函数以将一些数据传递给视图。它查询数据库中的 table 以获取血型作为标签,并获取“isAvailable = 1”处每种血型的行数。然后将这些数据传递到视图中以呈现图表。但如您所见,这些计数是错误的。即使“isAvailable = 0”,它也会对行进行计数。我的代码有什么问题,我该如何解决?
控制器中的函数。
public function bloodTypesChart()
{
$chartData = [];
$blood_types = $this->db->query("SELECT (BloodType) as blood_type FROM packets WHERE (isAvailable) = '1' GROUP BY blood_type")->result_array();
foreach($blood_types as $bt)
{
$record = $this->db->query("SELECT COUNT(PacketID) as count FROM packets WHERE BloodType = '{$bt['blood_type']}'")->result_array();
foreach($record as $row) {
$chartData['label'][] = $bt['blood_type'];
$chartData['data'][] = $row['count'];
}
}
$chartData['chart_data'] = json_encode($chartData);
$this->load->view('insight',$chartData);
}
查看
<script>
new Chart(document.getElementById("bar-chart"), {
type: 'bar',
data: {
labels: <?= json_encode($label)?>,
datasets: [
{
label: "Donations",
backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
data: <?= json_encode($data)?>
}
]
},
options: {
legend: { display: false },
title: {
display: true,
text: 'Donations'
}
}
});
</script>
在您的查询中,因为您在括号中封装了 only 字段名称,WHERE (isAvailable) = '1'
的计算结果为 WHERE there is a field labeled isAvailable
- 对于table。删除括号,它应该可以正常工作
$blood_types = $this->db->query("SELECT (BloodType) as blood_type FROM packets WHERE isAvailable = '1' GROUP BY blood_type")->result_array();
你这个方法太复杂了,避免使用php循环获取你可以通过简单查询获取的数据。
只需使用此 mysql 查询,计算 isAvailable 行数,当为真 (1) 时:
$sql=" SELECT BloodType as blood_type, COUNT(PacketID) as mycount
FROM packets
WHERE isAvailable = 1
GROUP BY blood_type
";
注意:我已将别名 count 更改为 mycount,因为 count 是保留字。
你的函数应该是这样的:
public function bloodTypesChart()
{
$sql=" SELECT BloodType as blood_type, COUNT(PacketID) as mycount
FROM packets
WHERE isAvailable = 1
GROUP BY blood_type
";
$chartData = [];
$blood_types = $this->db->query($sql)->result_array();
foreach($blood_types as $row)
{
$chartData['label'][] = $row['blood_type'];
$chartData['data'][] = $row['mycount'];
}
$chartData['chart_data'] = json_encode($chartData);
$this->load->view('insight',$chartData);
}
这里 sql-fiddle 使用您的数据库的简化版本执行查询