I welcome you. When you use PHP functions or methods, check what they return to know how to read what they bring.
In the case of mysqli_query
the Manual says that it returns this:
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
.
That is, before a query SELECT
there are two possible results:
-
FALSE
if the query is wrong
- An object% co_of% in case of success
In fact, the var_dump that samples are not the results of the query itself, but the mysqli_result
object that has been returned.
Now, that object must be read, using an appropriate method for it.
If you read the constitution of the objects mysqli_result
, you will see that you have several methods for read the data it brings (they are the methods that start with mysqli_result
), but it also has a property to know the number of rows that the object brought. For this case, since it is about knowing whether there are records or not, you can use the fetch_
property of the result object.
If it were to obtain more data, such as an array of results, then it would be convenient to use one of the methods mysqli_num_rows
of the class.
All this said, your code can be written like this:
$usuario = (empty($_POST['usuario'])) ? NULL : $_POST['usuario'];
$password = (empty($_POST['passWord'])) ? NULL : $_POST['passWord'];
$totalFilas=0;
if ($usuario){
include('connection.php');
if ($conexion){
if($existUser = mysqli_query($conexion, "SELECT * FROM datausers WHERE Usuario = '".$usuario."'")){
mysqli_store_result($conexion);
$totalFilas = mysqli_num_rows($existUser);
}else{
echo "Consulta errónea: ".mysqli_error($conexion);
}
}else{
echo "Conexión nula";
}
}else{
echo "No se posteó un nombre de usuario";
}
//Hacer algo con $totalFilas
If you want to verify the username and password at the same time, you can modify the first part of the code like this:
if ($usuario && $password){
include('connection.php');
if ($conexion){
$sql="SELECT * FROM datausers WHERE Usuario = '$usuario' AND Contraseña='$password'";
if($existUser = mysqli_query($conexion, $sql){
//todo el resto del código como más arriba