【问题标题】:MYSQL Syntax error - Codeigniter function to calculate costMYSQL 语法错误 - 计算成本的 Codeigniter 函数
【发布时间】:2012-01-12 17:39:12
【问题描述】:
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;
    }
} 
}

如果我将 $Q 更改为整数,则该函数可以正常工作。我还没有添加另一个站来减去差异来计算区域差异

将名称转换为区域 $station2 - $station1 = '计算的数字'

然后从 zone_cost 中选择成本 where zone = 'calculated 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;

【问题讨论】:

    标签: php mysql database forms codeigniter


    【解决方案1】:

    问题是变量$Q不包含有效值,大概是因为$station1传入的值在数据库中不存在。我原以为 CI 会通过至少使用一个空的引号字符串来解决这个问题,但显然不是。

    在将$Q 传递给where() 之前,您需要验证它是否具有合理的值。例如,另一个num_rows() 检查将告诉您第一个查询是否找到任何东西。

    另外,您需要从$Q 传递字段数据,而不仅仅是对象。所以在你的情况下,$Q->row()->Zone

    【讨论】:

    • 对,但当然在访问$QZone 属性之前,您首先需要访问该行。所以在这种情况下,正确的访问方式是$Q->row()->Zone
    • 好的,只有一个站,我如何减去 $station2 - $station 1 的输入得到 $Q ?我已经在帖子中发布了我的尝试
    【解决方案2】:

    $Q 是一个具有一些字段的对象,在这种情况下只有一个字段:Zone。请查看CodeIgniter Query results

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      • 2013-09-07
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 2016-08-23
      • 2012-07-09
      相关资源
      最近更新 更多