error while trying to log in

Asked

Viewed 61 times

0

I’m making a login system and when I press the button to log in it displays these two errors here

Warning: mysqli_query(): Couldn’t fetch mysqli in C: xampp htdocs 200cono vlogin.php on line 8

Warning: mysqli_num_rows() expects Parameter 1 to be mysqli_result, null Given in C: xampp htdocs 200cono vlogin.php on line 9

my login code and this one

<?php 

include("admin/bd/config.php");

if (isset($_POST['Usuario']) && isset($_POST['Senha'])) {
    $Usuario = $_POST['Usuario'];
    $Senha = $_POST['Senha'];
    $get = mysqli_query($con,"SELECT * FROM nlogin WHERE Usuario = '$Usuario'");
    $num = mysqli_num_rows($get);

    if ($num == 1) {

        while ($percorrer = mysqli_fetch_assoc($get)) {

            if (password_verify ( $_POST['Senha'] , $percorrer['Senha'] )){

                $adm = $percorrer['adm'];
                $Usuario = $percorrer['Usuario'];
                session_start();
                if ($adm == 1) {
                    $_SESSION['adm'] = $Usuario;
                    header("Location: admin/index.php");
                }else{
                    $_SESSION['nor'] = $username;
                    header("Location: index.php");
                }

            }

        }

    }


}

Code of the Config

<?php
$servername = "localhost";
$database = "site";
$username = "root";
$password = "";
// Create connection
$con = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$con) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
mysqli_close($con);
?>

Anyone can help?

  • It is error in consultation, mysqli_query($con,"SELECT * FROM nlogin WHERE Usuario = '$Usuario'") or die(mysqli_error($con)); see if there are any errors.

  • the errors persisted and now appeared this Notice: Use of Undefined Constant con - assumed 'con' in C: xampp htdocs 200cono vlogin.php on line 8

  • I think it would be interesting to also put the config.php

  • All right, I added

  • In your config file you are closing the connection at the end - mysqli_close($con). Remove this line and close the connection at the end of the login file.

  • Thanks, it worked out

  • @Bins, don’t you want to add your answer to the question win the 'answered' state? And @ Wicaro accept it as answer?

  • @Wesleygonçalves, I’ve already added the answer

Show 3 more comments

1 answer

1


The connection is being closed inside the "config" file, so the login file cannot run the query. Remove the line "mysqli_close($con);", your config file will look like this:

<?php
$servername = "localhost";
$database = "site";
$username = "root";
$password = "";
// Create connection
$con = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$con) {
    die("Connection failed: " . mysqli_connect_error());
}
?>

In the login file, close the connection at the end

<?php 

include("admin/bd/config.php");

if (isset($_POST['Usuario']) && isset($_POST['Senha'])) {
    $Usuario = $_POST['Usuario'];
    $Senha = $_POST['Senha'];
    $get = mysqli_query($con,"SELECT * FROM nlogin WHERE Usuario = '$Usuario'");
    $num = mysqli_num_rows($get);

    if ($num == 1) {

        while ($percorrer = mysqli_fetch_assoc($get)) {

            if (password_verify ( $_POST['Senha'] , $percorrer['Senha'] )){

                $adm = $percorrer['adm'];
                $Usuario = $percorrer['Usuario'];
                session_start();
                if ($adm == 1) {
                    $_SESSION['adm'] = $Usuario;
                    header("Location: admin/index.php");
                }else{
                    $_SESSION['nor'] = $username;
                    header("Location: index.php");
                }

            }

        }

    }
}
mysqli_close($con);
?>

Browser other questions tagged

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