【发布时间】:2017-02-01 18:13:29
【问题描述】:
我在 CodeIgniter 的模型中有这个查询:
function get_licenses_info($id)
{
$this->db->select('
vendors.id as vendors_id,
vendor_licenses.id as vendor_licenses_id,
vendor_licenses.vendor_id,
vendor_licenses.license_type,
vendor_licenses.date_expires,
vendor_licenses.license_num
');
$this->db->from('vendors');
$this->db->join('vendor_licenses', 'vendor_licenses.vendor_id = vendors.id', 'left');
$query = $this->db->get();
return $query->result_array();
}
在我看来,这段代码:
foreach($licenses as $l)
{
echo $l['license_type'];
if(strtotime($l['date_expires']) < time())
{
echo " expired on ".date("M, d, Y", strtotime($l['date_expires']))."<br>";
}
else
{
echo "<br>";
}
}
这就是视图中返回的内容:
Baldwin County
City of Pensacola expired on Jan, 04, 2017
expired on Dec, 31, 1969
我想也许我的数据库中以某种方式获得了额外的一行。但是我查了一下,只有我放在那里的两条记录。所以我想也许该表已损坏,将其删除并制作了一个带有 2 条新记录的新副本……同样的问题。 更奇怪的是,当我删除所有记录时,我得到的不是一个,而是两个“空白”迭代。 我对此进行了广泛的 google 搜索,但找不到 anything 发生这种情况,而不是通过搜索 CodeIgniter 的问题,甚至是一般的 mySQL 工作。任何建议将不胜感激。
这是 vendor_licenses 表:
╔════════════════════════════════════════════════════╦══╗
║ ║ ║
╠════════════════════════════════════════════════════╬══╣
║ id vendor_id license_type date_expires license_num ║ ║
║ 1 2 Baldwin County 2017-02-03 NULL ║ ║
║ 2 2 City of Pensacola 2017-01-04 NULL ║ ║
╚════════════════════════════════════════════════════╩══╝
这是供应商表:
+--------------+
| id is_active |
+--------------+
| 1 1 |
| 2 1 |
+--------------+
【问题讨论】:
-
所以问题是您不希望返回任何东西?
-
我希望返回数据库中存在的行,它们是,但最后返回的是不存在的行!我怀疑这与我的 foreach 循环有关......它至少循环了 1 次......
-
看起来像是加入问题。您介意包括
vendors和vendor_licenses中的所有条目吗? -
在循环查看
licenses中的内容之前,您是否检查过它?如果它已经包含您不期望的数据,则问题出在您的查询中。如果这是您所期望的,那么问题就出在您的循环中。 -
更新:将表格添加到 OP
标签: php mysql codeigniter