Problems with mysql and mysqli

mysql analyze
mysql analyze query
mysql explain
mysql optimize
mysql using
mysql explain plan
mysql index optimization
mysql query optimization

Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in C:\xampp\htdocs\swift\core\functions\general.php on line 49

Notice: Undefined variable: conn in C:\xampp\htdocs\swift\core\functions\users.php on line 85

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\swift\core\functions\users.php on line 85

Fatal error: Uncaught Error: Call to undefined function mysqli_result() in C:\xampp\htdocs\swift\core\functions\users.php:86 Stack trace: #0 C:\xampp\htdocs\swift\loginact.php(14): f_exists('') #1 {main} thrown in C:\xampp\htdocs\swift\core\functions\users.php on line 86

I keep on getting those errors but i cant find a way, i already tried everything.. Hopefully someone can help me as I am new to php. Ive been doing this for almost like a day now and i cannot figure out the answer.

This is the General.php.

<?php

     $connect_error = 'Sorry, there was some connectivity issue!';
    $conn = mysqli_connect('localhost','root','');
    $db = mysqli_select_db($conn, 'swift');


    function activation($to, $subject, $body) {
        mail($to, $subject, $body, 'From: swift@srikanthnatarajan.com');
    }

    function recovery_user_pass($to, $subject, $body) {
        mail($to, $subject, $body, 'From: swift@srikanthnatarajan.com');
    }

    function f_protect_page() {
        if(f_logged_in() === false) {
            header('Location: flogin.php');
            exit();     
        } 
    }

    function user_protect_page() {
        if(f_logged_in() === false) {
            header('Location: fprotect.php');
            exit();     
        } 
    }

    function use_protect_page() {
        if(f_logged_in() === true) {
            header('Location: fprotect.php');
            exit();     
        } 
    }

    function f_logged_in_redirect() {
        if(f_logged_in() === true) {
            header('Location: index.php');
            exit();
        }
    }

    function array_sanitize($item) {
        $item = htmlentities(strip_tags(mysqli_real_escape_string($item)));
    }

    function sanitize($data) {
        return htmlentities(strip_tags(mysqli_real_escape_string($data)));
    }

    function output_errors($errors) {
        return '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>';
    }

?>

this is the USERS.PHP can someone please check this.

<?php

$connect_error = 'Sorry, there was some connectivity issue!';
    $conn = mysqli_connect('localhost','root','');
    $db = mysqli_select_db($conn, 'swift');



    function f_recover($mode, $f_mailid) {
        $mode = sanitize($mode);
        $f_mailid = sanitize($f_mailid);
        $f_data = f_data(f_id_from_email($f_mailid),'f_id','f_fname','f_uname');
        if ($mode == 'f_uname') {
            recovery_user_pass($f_mailid, 'Recovery: Your username', "Hello " . $f_data['f_fname'] . ",\n\nYour username is: " . $f_data['f_uname'] . "\n\n-Swift Airlines");
        }
        else if($mode == 'f_password') {
            $generated_password = substr(md5(rand(999, 999999)), 0, 8);
            f_change_password($f_data['f_id'], $generated_password);

            update_f($f_data['f_id'], array('f_passrec' => '1'));

            recovery_user_pass($f_mailid, 'Recovery: Your password', "Hello " . $f_data['f_fname'] . ",\n\nYour new password is: " . $generated_password . "\n\n-TOFSIS");
        }
    }

    function f_activate($f_mailid, $f_mailcode) {
        $f_mailid = mysqli_real_escape_string($_POST['f_mailid']);
        $f_mailcode = mysqli_real_escape_string($_POST['f_mailcode']);
        if(mysqli_result(mysqli_query("SELECT COUNT(`f_id`) FROM `flight_users` WHERE `f_mailid` = '$f_mailid' AND `f_mailcode` = '$f_mailcode' AND `f_active` = 0"), 0) == 1) {
            mysqli_query("UPDATE `flight_users` SET `f_active` = 1 WHERE `f_mailid` = '$f_mailid' ");
            return true;
        }
        else {
            return false;
        }
    }

    function update_f($f_id, $update_data) {
        $update = array();
        array_walk($update_data, 'array_sanitize');
        foreach ($update_data as $field => $data) {
            $update[] = '`' . $field . '` = \'' . $data . '\'';
        }       
        mysqli_query("UPDATE `flight_users` SET " . implode(', ',$update) . "WHERE `f_id` = $f_id") or die(mysqli_error($conn));
    }

    function f_change_password($f_id, $f_password) {
        $f_id = (int)$f_id;
        $f_password = md5($f_password);
        mysqli_query("UPDATE `flight_users` SET `f_password` = '$f_password', `f_passrec` = 0 WHERE `f_id` = $f_id");
    }

    function register_f($register_data) {
        array_walk($register_data, 'array_sanitize');
        $register_data['f_fname'] = ucwords(strtolower($register_data['f_fname']));
        $register_data['f_lname'] = ucwords(strtolower($register_data['f_lname']));
        $register_data['f_password'] = md5($register_data['f_password']);
        $register_data['f_uname'] = strtolower($register_data['f_uname']);
        $fields = '`' . implode('`, `', array_keys($register_data)) . '`';
        $data = '\'' . implode('\', \'', $register_data) . '\'';

        mysqli_query("INSERT INTO `flight_users` ($fields, `f_regdate`) VALUES ($data, NOW())");
        activation($register_data['f_mailid'], 'Swift Airlines: Activate your account', "Hello " . $register_data['f_fname'] . ", \n\nYou need to activate your account in order to use the features of Swift Airlines. Please click the link below: \n\nhttp://srikanthnatarajan.com/swift/activate.php?f_mailid=" . $register_data['f_mailid'] . "&f_mailcode=" . $register_data['f_mailcode'] . " \n\n-Swift Airlines");
    }

    function f_data($f_id){
        $data = array();
        $f_id = (int)$f_id;
        $func_num_args = func_num_args();
        $func_get_args = func_get_args();
        if($func_num_args > 1) {
            unset($func_get_args[0]);
            $fields = '`'. implode('`, `', $func_get_args) . '`'; 
            $data = mysqli_fetch_assoc(mysqli_query("SELECT $fields FROM `flight_users` WHERE `f_id` = $f_id"));            
            return $data;
        }
    }

    function f_logged_in() {
        return (isset($_SESSION['f_id'])) ? true : false;
    }

    function f_exists($f_uname) {
        $f_uname = sanitize($f_uname);
        $query = mysqli_query($conn, "SELECT COUNT(`f_id`) FROM `flight_users` WHERE `f_uname`= '$f_uname'");
        return (mysqli_result($conn, $query, 0) == 1) ? true : false; 
    }

    function f_email_exists($f_mailid) {
        $f_mailid = sanitize($f_mailid);
        $query = mysqli_query($conn, "SELECT COUNT(`f_id`) FROM `flight_users` WHERE `f_mailid`= '$f_mailid'");
        return (mysqli_result($conn, $query, 0) == 1) ? true : false; 
    }

    function f_regid_exists($f_regid) {
        $f_regid = sanitize($f_regid);
        $query = mysqli_query($conn, "SELECT COUNT(`f_id`) FROM `flight_users` WHERE `f_regid`= '$f_regid'");
        return (mysqli_result($conn, $query, 0) == 1) ? true : false; 
    }

    function f_active($f_uname) {
        $f_uname = sanitize($f_uname);
        $query = mysqli_query($conn, "SELECT COUNT(`f_id`) FROM `flight_users` WHERE `f_uname`= '$f_uname' AND `f_active` = 1");
        return (mysqli_result($conn, $query, 0) == 1) ? true : false; 
    }

    function f_id_from_username($f_uname) {
        $f_uname = sanitize($f_uname);
        $query = mysqli_query($conn, "SELECT `f_id` FROM `flight_users` WHERE `f_uname` = '$f_uname'");
        return mysqli_result($conn, $query, 0, 'f_id');
    } 

    function f_id_from_email($f_mailid) {
        $f_mailid = sanitize($f_mailid);
        $query = mysqli_query($conn, "SELECT `f_id` FROM `flight_users` WHERE `f_mailid` = '$f_mailid'");
        return mysqli_result($conn, $query, 0, 'f_id');
    } 

    function f_login($f_uname, $f_password) {
        $f_id = f_id_from_username($f_uname);
        $f_uname = sanitize($f_uname);
        $f_password = md5($f_password);
        $query = mysqli_query($conn, "SELECT COUNT(`f_id`) FROM `flight_users` WHERE `f_uname`= '$f_uname' AND `f_password` = '$f_password'");
        return (mysqli_result($conn, $query, 0) == 1) ? $f_id : false;
    }

?>

and this is the Loginact.php------------------

<?php

        $title = 'Swift Airlines | Login Error';
    include $_SERVER["DOCUMENT_ROOT"].'/swift/core/init.php';


    if(empty($_POST) === false) {
        $f_uname = $_POST['f_uname'];
        $f_password = $_POST['f_password'];

        if(empty($f_uname) === true || empty($f_password) === true){
            $errors[] = 'You need to enter both, the username and the password!';
        } 
        else  if (f_exists($f_uname)===false) {
            $errors[] = 'No such user exists! Please register!';
        }
        else if(f_active($f_uname)===false) {
            $errors[] = 'Please activate your account!';
        }
        else {

            if(strlen($f_password)>32) {
                $errors[] = 'Password too long!';
            }

            $f_login = f_login($f_uname, $f_password);
            if($f_login===false) {
                $errors[] = 'Username and Password do not match!';
            }
            else {
                $_SESSION['f_id'] = $f_login;
                header('Location: http://localhost/swift/index.php');
                exit();
            }
        }       
    }
    else {
            $errors[] = 'No Log In credentials received!';
        }

    include $_SERVER["DOCUMENT_ROOT"].'/swift/includes/overall/header.php';

    if(empty($errors) === false) {
?>
    <br/><h4>We tried to log you in, but : </h4><br/>

<?php
    echo output_errors($errors);
    }

    include $_SERVER["DOCUMENT_ROOT"].'/swift/includes/overall/footer.php';

?>

I keep on getting those errors but i cant find a way, i already tried everything.. Hopefully someone can help me as I am new to php. Ive been doing this for almost like a day now and i cannot figure out the answer.

i know php is kind of hard yea and ive been looking for the answer for almost a day now. Im just new to programming and im doing my best to study hard about these kind of things hopefully someone can help me.

And i hope i can learn more about php and other programming language here.. ill keep looking for the answer even if im asking right now.. big thanks though.

I doubt you tried everything!

It looks as if you are trying to migrate from mysql_ functions to mysqli_.

From the manual for mysqli_fetch_array:

$query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3";
$result = mysqli_query($link, $query);

/* numeric array */
$row = mysqli_fetch_array($result, MYSQLI_NUM);
printf ("%s (%s)\n", $row[0], $row[1]);

$link is your mysqli connection. In your code it is $conn.

Within your functions you have a scope issue. $conn is in the global scope, so is not set within the function's scope.

Break it down, start with something like your f_id_from_email function, and follow and try to translate the manual's examples.

The error messages are actually quite helpful if read. But you are likely overwhelmed, as you are faced with many.

Call to undefined function mysqli_result()

That's because there is no mysqli_result function.

Top 10 MySQL Mistakes Made By PHP Developers, mysqli, or the MySQL improved extension, has several advantages: 99% of PHP performance problems will be caused by the database, and  Chapter 8 Common Problems with MySQL and PHP Error: Maximum Execution Time Exceeded : This is a PHP limit; go into the php.ini file and set Fatal error: Call to unsupported or undefined function mysql_connect Error: Undefined reference to 'uncompress' : This means that the client library is

1- general.php line 49: look at mysqli_real_escape_string, in procedural code, you must specify 2 arguments, the link (or connection) and the string.

function array_sanitize($conn,$item) {
        $item = htmlentities(strip_tags(mysqli_real_escape_string($conn,$item)));
    }

function sanitize($conn,$data) {
    return htmlentities(strip_tags(mysqli_real_escape_string($conn,$data)));
}

2- users.php, both on line 85, the $conn does not exist in the scope of the function. Pass $conn as an argument to the function and call it with ($conn,$f_uname).

function f_exists($conn,$f_uname) {
    $f_uname = sanitize($f_uname);
    $query = mysqli_query($conn, "SELECT COUNT(`f_id`) FROM `flight_users` WHERE `f_uname`= '$f_uname'");
    return (mysqli_result($conn, $query, 0) == 1) ? true : false; 
}

3- your question at line 86, it is the same as my #2 point.

Database problem with mysql instead of mysqli - Forum, I just installed Kunena a few days ago and it has an impressive feature set. However, I found one issue which is really bugging me. My page  If you have any other problems with the MySQL grant tables and ask on the MySQL Community Slack, always provide a dump of the MySQL grant tables. You can dump the tables with the mysqldump mysql command.

Notice: Undefined variable: conn ... Add in function global $conn. This is a very bad practice

function f_exists($f_uname, $conn) {
    // or global $conn;
    $f_uname = sanitize($f_uname);
    $query = mysqli_query($conn, "SELECT COUNT(`f_id`) FROM `flight_users` WHERE `f_uname`= '$f_uname'");
    return (mysqli_result($conn, $query, 0) == 1) ? true : false; 
}

problem with mysqli access to mysql, I can open a mysql DB table from php anbd run INSERT and SELECT queries on it with mysqli but somehow the damned library returns a  [RESOLVED] Some problems with mysqli. but if MySQL figurative chokes on it for some reason, then the PHP function returns a Boolean false. Therefore, you need to

PHP - Mysql To Mysqli Problems :(, PHP - Mysql To Mysqli Problems :( - Free PHP Programming Tutorials, Help, Tips​, Tricks, and More. 7.12 Common Problems with MySQL and PHP Error: Maximum Execution Time Exceeded : This is a PHP limit; go into the php.ini file and set the maximum execution time up from 30 seconds to something higher, as needed.

How to connect properly using mysqli, most common problems PHP developers stuck into when working with mysqli. bind_param() , giving you the actual error message from MySQL instead. PHP 7 no longer supports the mysql extension, so any legacy code using the mysql extension will need to be migrated to either PDO or mysqli before it can run on a server running PHP 7 or above. This article will give you examples of how to connect using mysqli and how to migrate the mysql methods to the mysqli procedural methods giving code

Connectors and APIs Manual :: 7.3.9.37 mysqli::query , mysqlnd_memcache_get_config · mysqlnd_memcache_set · Change History · PECL/mysqlnd_memcache 1.0 series · Common Problems with MySQL and PHP​. CentOS 5.8 and higher in a typical install of php has all the 'drivers' needed (php-mysql and php-mysqli - actually mysqli is include in the mysql driver). A CentOS server's php.ini file, BTW, shouldn't show any .dll's (that's a Windows php.ini).

Comments
  • where is html or css ?
  • FYI when you post line numbers (from errors), identify these lines in the code.
  • You can use classes and objects for it
  • Your sanitize function is filtering and escaping. It's trying to be a one size fits all magic sanitizer. Learn to escape appropriately.
  • This is a very bad practice... It is! Why are you recommending a very bad practice?
  • Just pass it as an argument, dont "global" it.
  • because the initial code is already bad. I have to show the best practice using the framework? Thank you for the minus
  • I have to show the best practice using the framework?... No, just read @Nic3500 comment
  • To the OP (Jie Eim), you accepted a wrong answer. Although it does work, you are learning the wrong method of doing it. OOP is the real solution. global (as ozornick pointed out rightly) is very bad practice.