Why do I receive different values in the same mysqli php query?

1

I have a problem with a mysqli login validation query, the problem is that there are two queries of the same function but obvious of different variables, one throws me an object result with an array of values, and the other returns bool ( false), the object array is the one that serves, because it validates the user name but the one of the password thrown by bool (false) does not validate but the user, even though entering an incorrect password, can go to the profile area.

My php code is this:

 <?php 

$usuario = $_POST['usuario'];
$password = $_POST['passWord'];

include('connection.php');

$existUser = mysqli_query($conexion, "SELECT * FROM datausers WHERE Usuario = '".$usuario."'");

$existPass = mysqli_query($conexion, "SELECT * FROM datausers WHERE Contraseña = '".$password"'");

var_dump($existUser);
var_dump($existPass);?>

What the $ existUser prints for me is:

  

object (mysqli_result) # 2 (5) {"current field"} = > int (0)   ["field_count"] = > int (7) ["lengths"] = > NULL ["num_rows"] = > int (0)   ["type"] = > int (0)}

What the $ existPass prints me = bool (false)

    
asked by Abdiel 26.11.2018 в 00:41
source

1 answer

0

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
    
answered by 26.11.2018 в 01:12