Q:

Mysql to Mysqli having hard time converting code

I'm watching some tutorials a couple years old by Alex of phpacademy.org and he is use mysql instead of mysqli I know mysqli is better and should be used instead but can't seem to figure out how to make this code work with all mysqli instead. Can someone convert it?
<?php
$connect = mysql_connect('localhost', 'root', 'pass') or die (mysqli_error());
mysql_select_db('jquery');

if (isset($_POST['username'])) {
    $username = mysql_real_escape_string($_POST['username']);
    if (!empty($username)) {
        $username_query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username`='$username'");
        $username_result = mysql_result($username_query, 0);

        if ($username_result == 0) {
            echo 'Username is available!';
        } else if ($username_result == 1) {
            echo 'Sorry, that username is taken.';
        }
    }
}
?>

What I tried to do is this.. but mysqli_result kept giving me errors:
<?php
$connect = mysqli_connect('localhost', 'root', 'pass', 'jquery') or die (mysqli_error());

if (isset($_POST['username'])) {
    $username = mysqli_real_escape_string($connect, $_POST['username']);
    if (!empty($username)) {
        $username_query = mysqli_query($connect, "SELECT COUNT(`user_id`) FROM `users` WHERE `username`='$username'");
        $username_result = mysqli_result($username_query, 0);

        if ($username_result == 0) {
            echo 'Username is available!';
        } else if ($username_result == 1) {
            echo 'Sorry, that username is taken.';
        }
    }
}
?>

so the only error I kept getting is from mysqli_result saying:

Fatal error: Call to undefined function mysqli_result() in \php\username.php on line 8

I looked up uses of mysqli_result online and haven't found many results to help me figure this out hopefully someone here can help me.

A:

Your $username_query is of the mysqli_result class when you use the object oriented style; mysqli_result is a class and not a function.
To get a row from your result set you can do (for example):
$username_result = mysqli_fetch_array($username_query);

And the reason you should use mysqli (or PDO) instead of the mysql_* functions is not just that the latter are deprecated: PDO (and mysqli) offer prepare statements and that offers better protection against sql injection than the escape functions.

