我使用CodeIgniter
和EasyAppointments
库进行调度程序活动。我不是CodeIgniter
的专家,我想知道如何通过查询执行来获取特定的行字段。我已经创建了ajax请求,模型作为文档描述,但现在我需要帮助才能做到这一点。首先,这是我传递给函数的内容(必须执行select查询):
Response => array(4) {
["nome"]=>
string(5) "Jack"
["cognome"]=>
string(6) "Mustrend"
["email"]=>
string(22) "jcsk@gmail.com"
["mobile"]=>
string(0) "0000"
}
$operatore_info
以上的变量包含名为users
的db表中可用的操作数的详细信息。该表的结构如下:
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(内部服务器错误)
执行ajax请求的地方:
var myObj = {};
myObj.nome = nome;
myObj.cognome = cognome;
myObj.email = email;
myObj.mobile = mobile;
var postUrl = GlobalVariables.baseUrl + 'backend_api/ajax_getSpecOpId';
var postData = { 'operatore_data': myObj };
$.post(postUrl, postData, function(response)
{
console.log("Response => " , response);
});
答案 0 :(得分: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 ;
}
答案 1 :(得分:0)
如果您在代码中遇到500内部服务器错误,则很可能语法问题或 SQL查询问题。请检查一下。
如果您使用AJAX,请检查控制台以获取所有PHP ERORR作为响应。
答案 2 :(得分:0)
我发现了这个问题,感谢所有这些让我朝着正确的方向前进。因此,在where
条款之后我们必须这样做:
$res = $this->db->get()->result_array();
而不是:
$res = $this->db->get('users');