Search code examples
phpmysqlilogicmysql-num-rows

Why my sql php logic return false output?


Hello I have this code and I have a problem at function evalLoggedUser I think...as it is now the function shows me wrong output(returning false instead true) and when I change the if statement inside the function and do it like this:

if($numrows == 0) {
    return true;
}

It work like as I wish...but I want to work like this and the if statement to be like if($numrows > 0). I am one whole day searching and trying to figure this out but nothing... I have tried to echo the $numrows var and does not echo nothing...also I echoed db errors and is all good. Any help would be appreciated. Thanks in advance.

<?php
session_start();
include_once("../db_includes/db_conx.php");
$user_ok = false;
$log_id = "";
$log_username = "";
$log_password = "";
// User Verify function
function evalLoggedUser($db_conx,$id,$u,$p){
    $sql = "SELECT ip FROM users WHERE id='$id' AND username='$u' AND password='$p' AND activated='1' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $numrows = mysqli_num_rows($query);
    if($numrows > 0){  
        return true;
    }
}
if(isset($_SESSION["userid"]) && isset($_SESSION["username"]) && isset($_SESSION["password"])) {
$log_id = preg_replace('#[^0-9]#', '', $_SESSION['userid']);
$log_username = preg_replace('#[^a-z0-9]#i', '', $_SESSION['username']);
$log_password = preg_replace('#[^a-z0-9]#i', '', $_SESSION['password']);
// Verify the user
$user_ok = evalLoggedUser($db_conx,$log_id,$log_username,$log_password);
} else if(isset($_COOKIE["id"]) && isset($_COOKIE["user"]) && isset($_COOKIE["pass"])){
$_SESSION['userid'] = preg_replace('#[^0-9]#', '', $_COOKIE['id']);
    $_SESSION['username'] = preg_replace('#[^a-z0-9]#i', '', $_COOKIE['user']);
    $_SESSION['password'] = preg_replace('#[^a-z0-9]#i', '', $_COOKIE['pass']);
$log_id = $_SESSION['userid'];
$log_username = $_SESSION['username'];
$log_password = $_SESSION['password'];
// Verify the user
$user_ok = evalLoggedUser($db_conx,$log_id,$log_username,$log_password);
if($user_ok == true){
// Update their lastlogin datetime field
$sql = "UPDATE users SET lastlogin=now() WHERE id='$log_id' LIMIT 1";
        $query = mysqli_query($db_conx, $sql);
}
}
?>

Solution

  • I found the solution...it seems that has not recognized the var $db_conx when I include the db_conx.php... So instead of include this file, I wrote the connection in the same folder that I showed you above and it works perfectly now:

    <?php
    session_start();
    include_once("../db_includes/db_conx.php");
    $user_ok = false;
    $log_id = "";
    $log_username = "";
    $log_password = "";
    // User Verify function
    function evalLoggedUser($db_conx,$id,$u,$p){
        $sql = "SELECT ip FROM users WHERE id='$id' AND username='$u' AND password='$p' AND activated='1' LIMIT 1";
        $query = mysqli_query($db_conx, $sql);
        $numrows = mysqli_num_rows($query);
        if($numrows > 0){  
            return true;
        }
    }
    

    to

    <?php
    session_start();
    $db_conx = mysqli_connect("xxxxxxxx","xxxxxxxx","xxxxxxxx","xxxxxxxx");
    // Evaluate the connection
    if (mysqli_connect_errno()) {
        echo mysqli_connect_error();
        exit();
    }
    $user_ok = false;
    $log_id = "";
    $log_username = "";
    $log_password = "";
    // User Verify function
    function evalLoggedUser($db_conx,$id,$u,$p){
        $sql = "SELECT ip FROM users WHERE id='$id' AND username='$u' AND password='$p' AND activated='1' LIMIT 1";
        $query = mysqli_query($db_conx, $sql);
        $numrows = mysqli_num_rows($query);
        if($numrows > 0){  
            return true;
        }
    }