构建查询时 CodeIgniter 模型方法出错

问题描述 投票:0回答:3

我有一个名为

users
的数据库表。该表的结构如下:

id
first_name
last_name
email
mobile_number
phone_number
address
city
state
zip_code
notes
id_roles
    

sql
中的查询应该是这样的:

SELECT id
FROM users
WHERE first_name = nome
  AND last_name = cognome
  AND email = email
  AND mobile_number = mobile

你怎么看我只想返回用户的ID,我怎样才能实现这一点?

更新可能的解决方案

public function returnSpecificOperatorId($operatore_information)
{
    //I parametri passati contengono i dettagli dell'operatore a cui dobbiamo prelevare l'id
    $nome = $operatore_information['nome'];
    $cognome = $operatore_information['cognome'];
    $email = $operatore_information['email'];
    $mobile = $operatore_information['mobile'];
    
    $this->db->select('id');
    $this->db->from('ea_users');
    $this->db->where(array(
        'first_name'    => $nome,
        'last_name'      => $cognome,
        'email' => $email,
        'mobile_number' => $mobile
    ));
    $res = $this->db->get('users');
    var_dump($res);
    return $res;
}

此代码返回我:

POST http://localhost/Calendario/backend_api/ajax_getSpecOpId 500(内部服务器错误)

php codeigniter activerecord query-builder
3个回答
1
投票

试试这个

$query = $this->db->query("SELECT id FROM users WHERE first_name = nome AND last_name = cognome AND email = email AND mobile_number = mobile");
$result = $query->result_array();
$count = count($result);

if(empty($count) || $count >1)
{
    echo "Invalid User";
}
else
{
    $id = $result[0]['id'];
    echo "User Id is ".$id ;
}

0
投票

如果代码中出现 500 内部服务器错误,则很可能存在 语法问题 SQL 查询问题 。请检查一下。

如果您使用 AJAX,请检查控制台以获取所有 PHP 错误响应。


0
投票

我发现了问题,感谢所有引导我走向正确方向的人。因此,在

where
子句之后我们必须这样做:

$res = $this->db->get()->result_array();

而不是这个:

$res = $this->db->get('users');
© www.soinside.com 2019 - 2024. All rights reserved.