带有codeigniter的CONCAT

时间:2012-03-08 01:49:55

标签: php codeigniter

我绝对没有看到为什么这不起作用的问题。关于可能性的任何想法?

$this->db->select(CONCAT_WS(' ', 'users.first_name', 'users.last_name') 'AS name');

编辑:

我使用建议的行更新但由于某种原因我仍然收到错误。

function getAllMessages($user_id)
{
    $this->db->select('pm.id');
    $this->db->select('pm.subject');
    $this->db->select("CONCAT_WS(' ', users.first_name, users.last_name) AS name");
    $this->db->select("DATE_FORMAT('pm.date_sent', '%M %D, %Y'");
    $this->db->select('pm.message_read');
    $this->db->from('users_personal_messages AS pm');
    $this->db->join('users', 'users.user_id = pm.sender_id');
    $this->db->where('recipient_id', $user_id);
    $query = $this->db->get();
    if ($query->num_rows() > 0)
    {
        return $query->result();;
    }
    else
    {
        return 0;
    }
}

更新:

function getAllMessages($user_id)
{
    $this->db->select('pm.id');
    $this->db->select('pm.subject');
    $this->db->select("CONCAT_WS(' ', users.first_name, users.last_name) AS name");
    $this->db->select("DATE_FORMAT('pm.date_sent', '%M %D, %Y')");
    $this->db->select('pm.message_read');
    $this->db->from('users_personal_messages AS pm');
    $this->db->join('users', 'users.user_id = pm.sender_id');
    $this->db->where('recipient_id', $user_id);
    $query = $this->db->get();
    if ($query->num_rows() > 0)
    {
        return $query->result();;
    }
    else
    {
        return 0;
    }
}

第二次更新:

function getAllMessages($user_id)
{
    $this->db->select('pm.id');
    $this->db->select('pm.subject');
    $this->db->select("CONCAT_WS(' ', users.first_name, users.last_name) AS name");
    $this->db->select(DATE_FORMAT(pm.date_sent, '%M %D, %Y'));
    $this->db->select('pm.message_read');
    $this->db->from('users_personal_messages AS pm');
    $this->db->join('users', 'users.user_id = pm.sender_id');
    $this->db->where('recipient_id', $user_id);
    $query = $this->db->get();
    if ($query->num_rows() > 0)
    {
        return $query->result();;
    }
    else
    {
        return 0;
    }
}

2 个答案:

答案 0 :(得分:5)

您需要正确引用字符串。不应引用列名,但必须引用select()的整个字符串参数。

$this->db->select("CONCAT_WS(' ', users.first_name, users.last_name) AS name");

有很多例子,请参阅CodeIgniter select() docs ...

更新

您的错误是DATE_FORMAT()行上缺少的括号:

$this->db->select("DATE_FORMAT(pm.date_sent, '%M %D, %Y')");
//------------------------------------------------------^^^^

答案 1 :(得分:0)

改变这个 $ this-> db-> select(“CONCAT_WS('',users.first_name,users.last_name)AS name”); 有了这个 $ this-> db-> select(“CONCAT_WS('',users.first_name,users.last_name)AS name”,FALSE);