Problems creating a login system with PHP and Mysqli

Asked

Viewed 1,826 times

0

I’m trying to make a system of login and registration of users in PHP, but I have several problems only in login.php, the registration is working correctly, but the login is presenting the errors: Warning: mysqli_query() expects Parameter 1 to be mysqli, null Given in C: wamp www rifa login.php on line 13 and Warning: mysqli_num_rows() expects Parameter 1 to be mysqli_result, null Given in C: wamp www rifa login.php on line 14, my code is just below, someone has an idea of how I can fix these errors?

<?php
error_reporting (E_ALL & ~ E_NOTICE & ~ E_DEPRECATED);
$hostname = 'localhost';
$username = 'root';
$password = '12345';
$db = 'rifou';

$connection = new mysqli($hostname, $username, $password);
mysqli_select_db($connection, $db);
$nickname = mysqli_real_escape_string($connection, $_POST['nick']);
$password = mysqli_real_escape_string($connection, $_POST['senha']);
$select_user = "SELECT * FROM usuarios WHERE nick='$nickname' AND senha='$senha'";
$run_user = mysqli_query($con, $select_user);
$check_user = mysqli_num_rows($run_user);
if ($check_user > 0){
    echo "logado";
} else {
   echo "não logado";
}
?>
  • Your connection is not valid.

  • What do you mean? The code cannot connect to db?

  • When I edited my code for the one described in http://www.w3schools.com/php/php_mysql_select.asp it worked perfectly, but not the way I want it to, but not the way I want it to work.

  • 1

    Do this in select, use the mysqli_query parameter, and call the $Connection variable first.

3 answers

2

Note that in your code the following line has been declared:

$connection = new mysqli($hostname, $username, $password);

Further down you stated:

$run_user = mysqli_query($con, $select_user);

The $con variable does not exist because you created it with the name $Connection. So it’s up to you to change the $Connection variable name to $con or $con to $Connection.

1

Warning: mysqli_query() expects Parameter 1 to be mysqli, null Given

Your connection is not valid according to the error. Your connection variable is $connection and not $con(on the call from mysqli_query().

$connection = new mysqli($hostname, $username, $password);
mysqli_select_db($connection, $db);
//Linhas omitidas
$run_user = mysqli_query($con, $select_user); //<--- deve $connection e não $con

-2

$password = mysqli_real_escape_string($connection, $_POST['senha']);
$select_user = "SELECT * FROM usuarios WHERE nick='$nickname' AND senha='$senha'";

you declared the variable as "$password" and when checking the table you placed "$password" a variable that does not exist

Browser other questions tagged

You are not signed in. Login or sign up in order to post.