我正在使用mysqli包装类。
更新mysql数据库的示例代码如下:
<?php
include('db.php');
$updatedata = array(
'user_type' => '15'
);
$where = array(
'member_id' => '5'
);
$update = $database->update('mysql_table_name', $updatedata, $where );
?>
现在上面的代码非常适合 member_id = 5
。
但是如何使用相同的上述功能更新**member_id != 5**
(不等于5 )的所有其他成员ID?
E.G。代码预期如下:
<?php
include('db.php');
$updatedata = array(
'user_type' => '15'
);
$where = array(
'member_id' ***is_not_equal_to*** '5'
);
$update = $database->update('mysql_table_name', $updatedata, $where );
?>
Mysqli包装类 - 更新功能代码如下:
public function update( $table, $variables = array(), $where = array(), $limit = '' )
{
self::$counter++;
//Make sure first that update data values are sanitized with above filter function
//Make sure the required data is passed before continuing
//This does not include the $where variable as (though infrequently)
//queries are designated to update entire tables
if( empty( $variables ) )
{
return false;
}
$sql = "UPDATE ". $table ." SET ";
foreach( $variables as $field => $value )
{
$updates[] = "`$field` = '$value'";
}
$sql .= implode(', ', $updates);
//Add the $where clauses as needed
if( !empty( $where ) )
{
foreach( $where as $field => $value )
{
$value = $value;
$clause[] = "$field = '$value'";
}
$sql .= ' WHERE '. implode(' AND ', $clause);
}
if( !empty( $limit ) )
{
$sql .= ' LIMIT '. $limit;
}
$query = $this->link->query( $sql );
if( $this->link->error )
{
$this->log_db_errors( $this->link->error, $sql );
return false;
}
else
{
return true;
}
}