Joe Bob Jr.
Joe Bob Jr.

Reputation: 41

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.

Upvotes: 1

Views: 535

Answers (2)

razz
razz

Reputation: 10110

Make sure first that the username column in your table is unique so that there wont be duplicate usernames and therefore $count would be exactly either 0 or 1, then try this:

if (isset($_POST['username']) && $_POST['username'] != '') {

    $connect = new mysqli('localhost', 'root', 'pass', 'jquery') or die (mysqli_error());
    $username       = $_POST['username'];
    $query          = "SELECT COUNT(`user_id`) AS count FROM `users` WHERE `username`=?";
    $username_query = $connect->prepare($query);
    $username_query->bind_param('s', $username);
    $username_query->execute();
    $username_query->bind_result($count);
    $username_query->fetch();
    $username_query->close();
    $connect->close();

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

Upvotes: 1

jeroen
jeroen

Reputation: 91734

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.

Upvotes: 2

Related Questions