When programming it is important to always keep in mind what the objects that we are using return, to evaluate them properly and write a controlled code. The PHP Manual that information is always detailed in the section called Return Values .
The PHP Manual in that section says that query()
:
Returns FALSE
in case of error. If a query of type SELECT
,
SHOW
, DESCRIBE
or EXPLAIN
is successful, mysqli_query()
will return a
object mysqli_result
. For other successful consultations of
mysqli_query()
will return TRUE
.
In your case, when you do not evaluate the result of the query you are trying to obtain using the query
of mysqli
method, you get this notice:
Trying to get property of non-object ...
It means that $resultado
is not a mysqli_result
object, because the query was not successful and therefore you can not use it to call the num_rows
method.
What went wrong in the query? The query has at least one syntax error (a comma before the FROM
.) But that is not the only possible failure in a query, so when you handle this type of objects you must write a code that evaluates the objects and tells you If something has failed and why it has failed, for that, in the code that I will propose, there will be a variable that will pick up the possible errors throughout the code.
Mysqli has its limitations, and one of them is how difficult it is to collect an array of data in prepared queries if you do not have the native driver called mysqlnd
. That's why an additional get_result
function is used here that will allow you to easily store your results in an organized array.
On the other hand, your code is insecure. Whenever dynamic data is involved in the query you should use prepared queries, since by allowing in the query data that other users can write, you could have a dangerous injection of code. It is a very high security risk that is often ignored when writing SQL queries.
Here is my code proposal:
$query="SELECT
tb_login.Usuario,
tb_login.Contra,
tb_login.Id
FROM
tb_login
WHERE tb_login.Usuario=?"; //'".$this->Usu_."'"; NUNCA PONGAS DATOS DINÁMICOS DIRECTAMENTE EN UNA CONSULTA
//instancia de las clases
$confi=new Datos_conexion();
$mysql=new mysqli($confi->host(),$confi->usuario(),$confi->pasword(),$confi->DB());
if($mysql){
$stmt=$mysql->prepare($query);
if ($stmt){
$stmt->bind_param("s", $this->Usu_);
$stmt->execute();
$arrResultado=get_result($stmt);
if ($arrResultado){
/*En este bloque se leerían los datos obtenidos*/
print_r($arrResultado);
}else{
$arrMensaje=array("mensaje"=>"No hay datos que cumplan el criterio de la consulta");
}
$stmt->close();
}else{
$arrMensaje=array("mensaje"=>"Hubo un fallo en la consulta ".$mysql->error);
}
/*Cerramos la conexión si no la vamos a usar en otras consultas*/
$mysql->close();
}else{
$arrMensaje=array("mensaje"=>"La conexión es nula");
}
/*Evaluamos si hubo error*/
if ($arrMensaje){
echo $arrMensaje["mensaje"];
}
/*Función auxiliar*/
function get_result( $Statement ) {
$RESULT = array();
$Statement->store_result();
for ( $i = 0; $i < $Statement->num_rows; $i++ ) {
$Metadata = $Statement->result_metadata();
$PARAMS = array();
while ( $Field = $Metadata->fetch_field() ) {
$PARAMS[] = &$RESULT[ $i ][ $Field->name ];
}
call_user_func_array( array( $Statement, 'bind_result' ), $PARAMS );
$Statement->fetch();
}
return $RESULT;
}