Php 如何在CodeIgniter中测试MySQL更新查询是否成功?

Php 如何在CodeIgniter中测试MySQL更新查询是否成功?,php,mysql,codeigniter,Php,Mysql,Codeigniter,我有一个模型函数,用于更新我的CodeIgniter应用程序中的用户: // updates first of a user, return true if successful, false if not. public function updateFirstName($userId, $newFirstName) { $this->db->query("UPDATE users SET firstName='$newFirstName' WHERE id=$userId

我有一个模型函数,用于更新我的CodeIgniter应用程序中的用户:

// updates first of a user, return true if successful, false if not.
public function updateFirstName($userId, $newFirstName) {
    $this->db->query("UPDATE users SET firstName='$newFirstName' WHERE id=$userId");
    return // whether request was successful?
}

如何返回一个布尔值以确保ID
$userId
的用户已被更新?例如,如果找不到ID为
$userId

的用户,它应该返回false。您可以使用
$this->db->infected_rows()
要检查查询是否成功运行,您是否尝试过
$this->db->infected_rows()


这将告诉您更新了多少行。

有关详细信息,请查看此项

通过这种方式,您还可以避免以前的sql注入

if ($this->db->affected_rows() > 0)
{
  return TRUE;
}
else
{
  return FALSE;
}

编辑

还有(好多了)


您可以在Codeigniter中使用
$this->db->infected_rows()
,当执行“写入”类型查询(插入、更新等)时,它将返回一个数值


在MySQL
DELETE FROM TABLE
中,返回0个受影响的行。数据库类有一个小漏洞,允许它返回正确数量的受影响行。默认情况下,此攻击已启用,但可以在数据库驱动程序文件中关闭。(来自CI用户指南)。对于Ci中已删除的行,它返回1

我发现一个更好的解决方案是管理一个错误和0个受影响行之间的差异。0受影响的行不一定是坏事,但错误是您确实想知道的:

if ($this->db->_error_message()) {
    return FALSE; // Or do whatever you gotta do here to raise an error
} else {
    return $this->db->affected_rows();
}
现在你的函数可以区分

if ($result === FALSE) {
    $this->errors[] = 'ERROR: Did not update, some error occurred.';
} else if ($result == 0) {
    $this->oks[] = 'No error, but no rows were updated.';
} else {
    $this->oks[] = 'Updated the rows.';
}
只是一些快速的黑客攻击——如果有其他人在使用代码,显然应该让代码更加冗长


问题是,考虑使用yError消息来区分0个更新行和一个实际问题。

< P>我使用这个代码检查更新查询。
$status = $this->db->query("UPDATE users SET firstName='$newFirstName' WHERE id=$userId"); 
if($status)
   return true;
else
    return false;
这将返回true或false

请尝试以下操作:

public function updateFirstName($userId, $newFirstName) {
    $this->db->where('id', $userId);
    $this->db->set('firstName', $newFirstName);
    $sql = $this->db->update('users');

    if ($sql) { return TRUE; }  // $sql - boolean true or false
}

使用存储过程,可以检查结果

下面是一个存储过程示例:

CREATE DEFINER=`root`@`localhost` PROCEDURE `usp_UpdateInfo`(tableId int,tableName varchar(100) charset utf8,description varchar(400) charset utf8)

BEGIN

    declare exit handler for sqlexception select 0 as `result`;

    update table
    set `name` = tableName,
        description = description
    where id = tableId;
    select 1 as `result` ;
END
PHP示例代码:

$this->load->database();

$rs = $this->db->query('call usp_UpdateInfo(?,?,?)',array($tableId,$tableName,$description));
$this->db->close();     
return $rs->result_array();

您是否尝试过$this->db->impacted_rows()?这将告诉您更新了多少行。很好的SQL注入漏洞。我更担心您的数据库是pwn3d,而不是查询成功/失败。$userId=“1或1=1”;应该这样做。@ChrisK如果您可以更改您的用户ID,您可能不需要使用SQL injectionor或简单地返回(bool)($this->db->infected_rows()>0);这篇文章完美地代表了初级php开发人员的发展:Dor this too$这->db->impacted_rows()impacted row=0,并不意味着有错误。但是,如果用户提交的数据没有任何更改怎么办。在这种情况下,将找到数据,但受影响的行仍将为0。即使没有更新行,也无法检查,这并不意味着查询失败。您可以通过“上次更新”时间戳扩展数据库列,因此将至少有一个更新列,并且
$this->db->infected_rows()
的结果为0。
返回(bool)$status;
将在此处为您节省三行。我不确定,
如果($result==0){$this->oks[]='没有错误,但没有更新行。;}
对我来说似乎无效。
($result==1)
用于即使更新了0行也没有错误的成功查询。
($result==0)
用于错误查询-
mysql\u受影响的行()
有效!!
$status = $this->db->query("UPDATE users SET firstName='$newFirstName' WHERE id=$userId"); 
if($status)
   return true;
else
    return false;
public function updateInfo($newinfo) {
    $this->db->update("some_table", $newinfo);
    return ($this->db->affected_rows() > 0);
}
public function updateFirstName($userId, $newFirstName) {
    $this->db->where('id', $userId);
    $this->db->set('firstName', $newFirstName);
    $sql = $this->db->update('users');

    if ($sql) { return TRUE; }  // $sql - boolean true or false
}
CREATE DEFINER=`root`@`localhost` PROCEDURE `usp_UpdateInfo`(tableId int,tableName varchar(100) charset utf8,description varchar(400) charset utf8)

BEGIN

    declare exit handler for sqlexception select 0 as `result`;

    update table
    set `name` = tableName,
        description = description
    where id = tableId;
    select 1 as `result` ;
END
$this->load->database();

$rs = $this->db->query('call usp_UpdateInfo(?,?,?)',array($tableId,$tableName,$description));
$this->db->close();     
return $rs->result_array();