Error [false] when making a query (MySQL - PHP - PHPMyAdmin)

1

my query is at the end.

<?php

$usu = $_REQUEST["usu"];
$con = $_REQUEST["con"];


$conexion = mysqli_connect("localhost","root","mysql","android");    

$res = mysqli_query($conexion, "select * from profesor
where usuario='$usu' and contra='$con'")or die(mysqli_error($res));    

/*'".mysqli_real_escape_string($conexion, $usu)."'*/

$q = "select * from profesor where usuario = '$usu' and contra = '$con'"."   <br/>";

echo "ESTO SALE DE LA VARIABLE sq Y CONTIENE --> $q";

if($res == null){
 echo "No hay nadie con el usuario y contraseña insertado";
 }else{
 echo "Sí hay alguien con el usuario y contraseña insertado"."<br/>";
 }


$datos = array();

foreach ((array) $res as $row){
    $datos[]=$row;
}

 $final = json_encode($datos);

echo $final;


?>

This is my current BD, this bd is just a test, to practice, so I can show it quietly:

Now the following image is this: The first line is to see if there is someone with the user and password inserted, and correctly I know that it exists. The second is the if I did to see if there is record found in the variable "q". This sentence is shown to me in my browser "select * from teacher where user = 'gezer' and against = 'hello'" I have run it in phpmyadmin and it shows me the field correctly.

I hope you let me understand, now my query is because in line 3 I get: [], as if the arrangement was empty, I think I would have to leave the data of the person with that username and password.

    
asked by Jorge Requez 08.11.2016 в 23:24
source

2 answers

0

First, never from the nuncas you make queries to DB with data coming from the user without validating them and healing them since as you have it you are vulnerable to sql injection.

Second,

die('HERE ERROR!!!!'.mysql_error());

You are mixing the mysqli extension with the obsolete mysql, here the reference that is extreme It is recommended that you take a look. The best thing is that you use mysqli_error:

if (!mysqli_query($link, "SET a=1")) {
    printf("Errormessage: %s\n", mysqli_error($link));
}

(Example taken from the official documentation ).

Third, the error may be because your statement returns null. How to check it?

You can do the following:

$query = "select * from profesor
where usuario='$usu' and contraseña='$con'"; 

echo $query;

It will print the query you do to mysql, then the copies and execute it in Phpmyadmin directly in the part that says SQL . If the query is wrong there you will know, if it is ok then it is an error of your code, but I think that it is most likely from the SELECT.

You tell how it went.

Edit after knowing that the query is correct

The array prints empty because in effect, nothing is added to it. Why? It seems like the way you do the foreach.

Only to understand what happens, to test do the following:

var_dump($res);

You will see that it is a resource, that is, a result of a query. The next step is to pass your content to a data structure like an array.

To do this, try replacing the foreach with the following:

// Segundo parámetro MYSQLI_ASSOC te dará un array asociativo
   $row = mysqli_fetch_array($res, MYSQLI_ASSOC);

// Te dará array numérico
$row = mysqli_fetch_array($result, MYSQLI_NUM);

// Para que veas el valor final
var_dump($row);

And I think that's enough with that. It seems that you need a review of Php with Mysqli, I do not know how to do this, but enter my profile here and go to my links, I do tutorials and courses on these topics, they can help you.

    
answered by 09.11.2016 / 00:51
source
0

Given that the sql statement returns it well, I think that the problem is when you pick up each row of the query. To do this, you could use the function mysqli_fetch_array :

while ($fila = mysqli_fetch_array($res, MYSQLI_BOTH)) {
    print_r($fila);
}

To the function mysqli_connect you could also pass the name of the base of data as a parameter:

$conexion = mysqli_connect("localhost","root","mysql","android");

Therefore, you could remove the mysqli_select_db function from your program.

    
answered by 08.11.2016 в 23:26