[英]PHP affected_rows return 0 but query executes
I have a function where I do an update to a table, after I call it I check the table and the row has changed but the functions returns "Error at update" instead of "Doctor occupied". 我有一个对表进行更新的函数,调用它后我检查表并更改了行,但是函数返回“更新时出错”而不是“医生已占用”。 Here is my function. 这是我的功能。
function funcion(){
$con = new Conexion();
$con->conecta();
$sql = “SELECT * FROM llamada WHERE id_llamada = $id_llamada AND id_medico = $id_medico”;
$res = $con->consulta($sql);
if ($res->num_rows > 0) {
$sql = “UPDATE medico SET disponible = 1 WHERE id_medico = $id_medico”;
$con->consulta($sql);
// var_dump($con->mysqli->affected_rows);
if ($con->mysqli->affected_rows() > 0) {
return [true,‘Doctor occupied’];
}else{
return [false,‘Error at update’,$con->mysqli];
}
}else{
return [false,‘Data doesn't match’];
}
}
The function conecta() does the connection with mysql. 函数conecta()与mysql建立连接。
Here is the class. 这是课程。
class Conexion {
public $mysqli;
public function conecta(){
$this->mysqli = new mysqli(‘SERVER’, ‘USER’, ‘PASSWORD’, ‘DATABASE’);
if ($this->mysqli->connect_errno) {
echo “Fallo al conectar a MySQL: (” . $this->mysqli->connect_errno . “) ” . $this->mysqli->connect_error;
}
//echo $this->mysqli->host_info . “<br>“;
}
public function desconecta(){
$this->mysqli->close();
}
public function consulta($sql) {
$resultado = $this->mysqli->query($sql);
if (!$resultado) {
echo “Error en la consulta <br>“;
}
if (is_null($resultado)){
echo “sin resultados”;
}else{
return $resultado;
}
}
}
Assuming that $con->mysqli
is the connection handle, affected_rows
is a property and not a method. 假设$con->mysqli
是连接句柄,那么affected_rows
是属性而不是方法。 So your line... 所以你的电话...
if ($con->mysqli->affected_rows() > 0) {
should be 应该
if ($con->mysqli->affected_rows > 0) {
Which is how you use it in... 您如何在...中使用它?
// var_dump($con->mysqli->affected_rows);
mySqli offers two interfaces, one object oriented and the other procedural. mySqli提供了两个接口,一个是面向对象的接口,另一个是过程接口。 In the object-oriented manner, affected_rows is indeed a property of the mySqli object. 以面向对象的方式,受影响的行确实是mySqli对象的属性。 However, if you are more comfortable using procedural coding, then one could use the function mysqli_affected_rows
() which needs a $link
parameter (see more info here ). 但是,如果您更习惯使用过程编码,则可以使用函数mysqli_affected_rows
(),该函数需要一个$link
参数(请参见此处的更多信息)。
An example of the procedural option: 程序选项的一个示例:
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
// code for a SELECT, INSERT, UPDATE, REPLACE, or DELETE query
printf("Affected rows: %d\n", mysqli_affected_rows($link) );
Note: you may also find a good tutorial on the procedural style here . 注意:您还可以在此处找到有关程序样式的很好的教程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.