Similar Posts

22 thoughts on “PHP Tutorials: Register & Login (Part 5): User Login (Part 2)
  1. This is written in php 5 and most of you will be using php 7 which does not support a lot of the "mysql" functions such as mysql_query and instead you will get away with "mysqli" instead such as mysqli_query. HOWEVER this is not true for mysql_result.

    Thanks to Ivan F for the answer!!!!
    PHP5 code:
    function user_exists($username) {

    $username = sanitize($username);

    $query = mysqli_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'");

    return(mysqli_result($query, 0) == 1) ? true : false;

    }

    Change to PHP7 code:
    function user_exists($username) {

    $conn = mysqli_connect('localhost','root','','lr');

    if (mysqli_num_rows(mysqli_query($conn,"SELECT * FROM users WHERE username = '$username' ")) > 0) {

    return true;

    } else {

    return false;

    }

    }

    Also add

    elseif (!user_exists($username)) {

    echo "No username!";

    }

  2. I´m getting this message when i click Login button:

    Deprecated: mysql_real_escape_string(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in C:xampphtdocsintranetportalcorefunctionsgeneral.php on line 3

    Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:xampphtdocsintranetportalcorefunctionsusers.php on line 6

  3. alternative for mysqli without using mysql_result is this:
    function user_exists($username) {
    $conn = mysqli_connect('localhost','root','','lr');
    if (mysqli_num_rows(mysqli_query($conn,"SELECT * FROM users WHERE username = '$username' ")) > 0) {
    return true;
    } else {
    return false;
    }
    }

    And in the control login need to put this:
    elseif (!user_exists($username)) {
    echo "No username!";
    }

  4. My brain … pls help
    Warning: mysql_real_escape_string() expects parameter 1 to be string, object given in /core/functions/general.php on line 4
    – – – – – –
    general.php
    <?php
    function sanitize($data){
    include('core/database/connect.php'); // $link = mysqli_connect($host,$user,$password,$db);
    return mysql_real_escape_string($link,$data);
    }
    ?>
    – – – – – – —
    users.php
    <?php

    function user_exists($username) {
    include('core/database/connect.php');
    $username = sanitize($username);
    $sql = "SELECT * FROM users WHERE username = '". $username ."'";
    $query = mysqli_query($link, $sql);
    return (mysqli_result($query, 0) == 1) ? true:false;
    }

    function mysqli_result($res,$row=0,$col=0){
    $numrows = mysqli_num_rows($res);
    if ($numrows && $row <= ($numrows-1) && $row >=0){
    mysqli_data_seek($res,$row);
    $resrow = (is_numeric($col)) ? mysqli_fetch_row($res) : mysqli_fetch_assoc($res);
    if (isset($resrow[$col])){
    return $resrow[$col];
    }
    }
    return false;
    }

    function user_active($username) {
    include('core/database/connect.php');
    $username = sanitize($username);
    $sql = "SELECT * FROM users WHERE username = '$username'AND active=1";
    $query = mysqli_query($link, $sql);
    return (mysql_num_rows($query) > 0) ? true:false;
    }

    function user_id_from_username($username) {
    include('core/database/connect.php');
    $username = sanitize($username);
    $sql = "SELECT `user_id` FROM `users` WHERE `username` = '$username' ";
    $query = mysqli_query($link, $sql);
    return mysqli_result($query, 0, 'user_id');
    }

    function login($username, $password) {
    include('core/database/connect.php');
    $user_id = user_id_from_username($username);
    $username = sanitize($username);
    $password = md5($password);
    $sql = "SELECT COUNT(user_id) FROM `users` WHERE `username` = '$username' AND `password` = '$password' ";
    $query = mysqli_query($link, $sql);
    return (mysqli_result($query, 0) == 1) ? $user_id : false;
    }

    ?>

  5. solution:
    $query = mysql_query("SELECT COUNT (`user_id`) FROM `user` WHERE `username`='$username'");
    return (mysql_result ($query, 0)=='1'? true:false);
    ————
    Put the condition in the ternary operator in single quotes. It seems the mysql_result () function is returning a string and not an integer or boolean type. So use single quotes in the condition.

  6. i cant fix this error. Can you guys help me how to fix this? this is the error Fatal error: Can't use function return value in write context in C:xampphtdocsthesislogin.php on line 13

    im having a hard time thingking about that error.

  7. I am using wampserver 3.0.6, I think it doesn't allow mysql_result nor mysql_query to use but suggested to use mysql_result and mysqli_qquery instead. is there any other solution?

  8. somebody help

    <?php
    function sanitize($data) {
    return mysqli_real_escape_string($data);
    }
    ?>

    keep showing
    Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string
    given in D:wamp64wwwprojectcorefunctiongeneral.php on line 3

  9. Sanitize doesn't work.

    Notice: Undefined variable: db in C:xampphtdocsgeneral.php on line 3

    Fatal error: Call to a member function return() on null in C:xampphtdocsgeneral.php on line 3

    Keep in mind. I wrote my script to connect my db to the server like this.

    $db = new mysqli('localhost','root','','(the name of the database);

    if ($db->connect_errno){
    die('')
    }

    What gives? What's going on with it?

  10. i edit the code on users.php and now it's work successfully
    <?php
    function logged_in(){
    return (isset($_SESSION['user_id'])) ? true : false;
    }
    function user_exists($username){
    $username = sanitize($username);
    $query = "SELECT * FROM users WHERE username = '" . $username . "'";
    if($result = mysqli_query($GLOBALS['db'], $query)){
    if(mysqli_num_rows($result) == 1){
    return true;
    }else{
    return false;
    }
    mysqli_free_result($result);
    }
    }
    function user_active($username){
    $username = sanitize($username);
    $query = "SELECT * FROM users WHERE username = '" . $username . "'";
    if($result = mysqli_query($GLOBALS['db'], $query . 'And active = 1')){
    if(mysqli_num_rows($result) == 1){
    return true;
    }else{
    return false;
    }
    mysqli_free_result($result);
    }
    }
    function user_id_from_username($username){
    $username = sanitize($username);
    $query3 = "SELECT user_id FROM users WHERE username = '" . $username . "'";
    if($result = mysqli_query($GLOBALS['db'], $query3)){
    if(mysqli_num_rows($result) == 1){
    while ($obj = mysqli_fetch_object($result)) {
    return $obj->user_id;
    }
    }
    mysqli_free_result($result);
    }
    }

    function login($username, $password){
    $user_id = user_id_from_username($username);
    $username = sanitize($username);
    $password = md5($password);
    $query = "SELECT * FROM users WHERE username = '" . $username . "' AND password = '" . $password . "'";
    if($result = mysqli_query($GLOBALS['db'], $query)){
    if(mysqli_num_rows($result) == 1){
    return $user_id;
    }else{
    return false;
    }
    mysqli_free_result($result);
    }
    }
    ?>
    and the code on connect.php shold be like this
    <?php
    $db = mysqli_connect('127.0.0.1','root','root','login_register');
    if(mysqli_connect_errno()){
    die('sorry we have some problems');
    }
    ?>

  11. Whenever i use a username from the database i get user_exists message even if the username and password are correct
    it doesn't show user_active message …
    someone plz help

  12. anyone help please
    Cannot redeclare user_exists() (previously declared in C:xampphtdocsxamppsitecorefunctionsusers.php:3) in C:xampphtdocsxamppsitecorefunctionsusers.php on line 21

Leave a Reply to Muhammad Khawer Abbas Naqvi Cancel reply

Your email address will not be published. Required fields are marked *