【问题标题】:How to get all alphabetic letters in codeigniter from DB? PHP, MySQL, CodeIgniter如何从DB中获取codeigniter中的所有字母? PHP、MySQL、CodeIgniter
【发布时间】:2013-11-28 00:31:46
【问题描述】:

我正在使用它来获取所有字母以创建字母列表: SELECT DISTINCT SUBSTRING(title, 1, 1) AS letter FROM games ORDER BY letter asc 一切还好。但后来我尝试将它传递给 codeigniter,它失败(尝试)了几个变体:

$this->db->distinct();
$this->db->select("SUBSTRING(title,1,1) AS letter");
$this->db->order_by("letter", "asc");
$query = $this->db->get('games');

还有这个:

$this->db->query("SELECT DISTINCT SUBSTRING(title,1,1) AS letter ORDER BY letter");
$query = $this->db->get('games');

我得到的所有时间:

错误号:1064

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'AS letter FROM (`games`) ORDER BY `letter` asc' 附近使用正确的语法

SELECT DISTINCT SUBSTRING(title, `1`, `1)` AS letter FROM (`games`) ORDER BY `letter` asc

文件名:E:\wamp\www\gamecenter\system\database\DB_driver.php

行号:330

我可以尝试什么来解决这个问题?

【问题讨论】:

  • 所以您没有发现生成的查询有什么问题?
  • 报价?如果它们是由 codeigniter 生成的,我该如何删除它们?
  • 查看 CI 文档,了解如何将函数及其参数传递到查询中

标签: php mysql codeigniter


【解决方案1】:

使用 FALSE 将第二个参数传递给选择

$this->db->select("SUBSTRING(title,1,1) AS letter", FALSE); //This way it will not put the ` characters.

【讨论】:

    【解决方案2】:

    $result = $this->db->query('SELECT DISTINCT SUBSTRING(title,1,1) AS letter FROM (games) ORDER BY letterasc')->result();

    我认为它对你有帮助。

    【讨论】:

      猜你喜欢
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      • 2012-05-02
      • 1970-01-01
      • 2022-06-16
      • 2017-11-30
      • 1970-01-01
      • 2013-05-03
      相关资源
      最近更新 更多