很好的一天。
在数据库中创建查询函数:
function mysqli($query){
$mysqli = new mysqli('test','test','test','test');
if (mysqli_connect_errno()) {
printf("Bad connect: %s\n", mysqli_connect_error());
exit();
}
$result = $mysqli->query("SET NAMES utf8");
$result = $mysqli->query("set character_set_client='utf8'");
$result = $mysqli->query("set collation_connection='utf8_general_ci'");
$result = $mysqli->query($query);
//$mysqli->error;
//$result->error;
$mysqli->close();
return $result;
};
在下一步中,我要计算受影响的行数。
为此我做了:
$res2 = mysqli("INSERT INTO Table (name, value) VALUES ('$name', '$value')");
echo $res2->affected_rows;
但我在网上
请告诉我如何正确计数?
最佳答案
删除行:
$mysqli->close();
从函数中。这样就行了。
function mysqli($query){
$mysqli = new mysqli('test','test','test','test');
if (mysqli_connect_errno()) {
printf("Bad connect: %s\n", mysqli_connect_error());
exit();
}
$result = $mysqli->query("SET NAMES utf8");
$result = $mysqli->query("set character_set_client='utf8'");
$result = $mysqli->query("set collation_connection='utf8_general_ci'");
$result = $mysqli->query($query);
$arr = array($result,$mysqli);
return $arr;
}
像这样的用法:
$res2 = mysqli("INSERT INTO Table (name, value) VALUES ('$name', '$value')");
echo $res2[1]->affected_rows;
你的结果将是这个变量:
res2[0];
阅读此答案:mysqli_affected_rows() expects parameter 1 to be mysqli, object given
关于php - php和mysqli上受影响的行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19590078/