MYSQL语法错误 - Codeigniter函数计算成本 [英] MYSQL Syntax error - Codeigniter function to calculate cost
问题描述
function stationcost($station1,$station2)
{
$data = array();
$this->db->select('Zone')->from('station_zone')->where('Station', $station1);
$Q = $this->db->get();
$this->db->select('cost')->from('zone_cost')->where('zone', $Q);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data = $row->Cost;
return $data;
}
}
}
一个整数,该函数正确工作。
If i change $Q to an integer, the function works correctly. I'm yet to add another station to minus the difference to calculate the zone difference
将名称转换为区域
$ station2 - $ station1 ='computedfigure'
Convert names to zones $station2 - $station1 = 'calculatedfigure'
然后从zone_cost中选择成本,其中zone ='计算图';
Then select cost from zone_cost where zone = 'calculcated figure';
我的错误消息:
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
SELECT `cost` FROM (`zone_cost`) WHERE `zone` =
Filename: models/station_model.php
Line Number: 59
我试过了这个
$this->db->select('Zone')->from('station_zone')->where('Station', $station2);
$S2 = $this->db->get();
$this->db->select('Zone')->from('station_zone')->where('Station', $station1);
$S1 = $this->db->get();
$Q = $S2 - $S1;
推荐答案
问题是变量 $ Q
不包含有效值,可能是因为在 $ station1
中传递的值在数据库中不存在。
The problem is that the variable $Q
does not contain a valid value, presumably because the value passed in $station1
does not exist in the database. I would have thought that CI would counter this by at least using an empty quoted string, but apparently not.
你需要验证 $ Q
在传递给 where()
之前具有合理的值。例如,另一个 num_rows()
检查将告诉您第一个查询是否找到任何东西。
You need to validate that $Q
holds a sensible value before you pass it to where()
. For example, another num_rows()
check will tell you whether the first query found anything.
从 $ Q
传递字段数据,而不仅仅是对象。因此,在您的情况下, $ Q-> row() - > Zone
。
Also, you need to pass the field data from $Q
, not just the object. So in your case, $Q->row()->Zone
.
这篇关于MYSQL语法错误 - Codeigniter函数计算成本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!