warning: mysqli_query expects parameter

1

Hi, try to declare these parameters but it seems to cause me an error, originally they were all mysql and I changed them to mysqli. Could you tell me where I'm wrong?

LINES OF THE CODES THAT CAUSE THE ERROR

$sql2=mysqli_query("SELECT * FROM usuarios WHERE Correo='$username'");
    if($f2=mysqli_fetch_array($sql2)){</code>
 $sql=mysqli_query("SELECT * FROM prueba WHERE Correo='$username'");
    if($f=mysqli_fetch_array($sql)){</code>

The code is a login that identifies email and password written with the BDD

require('conexion/conex.php');

$username=$_POST['mail'];
$pass=$_POST['pass'];



$sql2=mysqli_query("SELECT * FROM usuarios WHERE Correo='$username'");
if($f2=mysqli_fetch_array($sql2)){
    if($pass==$f2['pasadmin']){
        echo '<script>alert("BIENVENIDO ADMINISTRADOR")</script> ';

        echo "<script>location.href='admin.php'</script>";

    }
}




$sql=mysqli_query("SELECT * FROM prueba WHERE Correo='$username'");
if($f=mysqli_fetch_array($sql)){
    if($pass==$f['Contraseña']){
        header("Location: index2.php");
    }else{
        echo '<script>alert("CONTRASEÑA INCORRECTA")</script> ';

        echo "<script>location.href='index.php'</script>";
    }
}else{

    echo '<script>alert("ESTE USUARIO NO EXISTE, PORFAVOR REGISTRESE PARA PODER INGRESAR")</script> ';

    echo "<script>location.href='index.php'</script>";  

}
    
asked by KJSK 15.02.2018 в 22:32
source

3 answers

2

The mysqli_query function requires that you pass as a parameter, an identifier returned by mysqli_connect or mysqli_init, do not pass us the conex.php file, but it follows that it will have something like:

$link = mysqli_connect("localhost", "my_user", "my_password", "world");

Then the line that fails you will be like this:

$sql2 = mysqli_query($link, "SELECT * FROM usuarios WHERE Correo='$username'");

Another way to do it would be like this:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$mysqli->query("SELECT * FROM usuarios WHERE Correo='$username'");

In any case, you need to review documentation of two things:

link

How to avoid SQL injection in PHP?

    
answered by 15.02.2018 / 22:46
source
0

As you use mysqli_fetch_array in function format, you need to pass the connection as first parameter:

$link = mysqli_connect("localhost", "my_user", "my_password", "world");
$sql2 = mysqli_query($link,"SELECT * FROM usuarios WHERE Correo='$username'");

You have more information here

    
answered by 15.02.2018 в 22:44
0

The error says that the function expects 2 input parameters, although it really accepts 3, you are passing it only 1 parameter.

  

connection (REQUIRED) : The MySQL connection on which the function will work.

     

query (REQUIRED) : The query that will be sent to the database.

     

Mode (OPTIONAL) : The constant MYSQLI_USE_RESULT or MYSQLI_STORE_RESULT depending on the desired behavior.

The correct call for the mysqli_query() function is:

mysqli_query($conexion, $query);

In your case:

$conexion = mysqli_connect("localhost","user","pass","db");
$query = mysqli_query($conexion, "SELECT * FROM usuarios WHERE Correo='$username'");

You can check simple examples at w3schools.com

    
answered by 15.02.2018 в 22:50