user2835191
user2835191

Reputation:

PHP undefined mysqli_result

I'm checking the database if a username is taken, but I keep getting this error "Call to undefined function mysqli_result() on line 9.

<?
$connect = mysqli_connect('localhost', 'root', '', 'mydb');
if(isset($_POST['username'])){
  $username = mysqli_real_escape_string($connect, $_POST['username']);
  if (!empty($username)){
    $username_query = mysqli_query($connect, "SELECT COUNT ('id') FROM 'users' WHERE 'username' = '$username'");
    $username_result = mysqli_result($username_query, 0);

    if ($username_result == 0){
        echo 'Available.';
        } else if($username_result == 1){
        echo 'Username unavailable.';
    }
  }
}
?>

Perhaps because I'm using jQuery to handle the input from a different file and checking it with this, it's saying that it's undefined. This same code works fine in the tutorial that I'm watching, but he is using mysql instead of mysqli. Edit: The fixed code looks like replaces $username_query and $username_rsult -

    $data = mysqli_query($connect, "SELECT COUNT(`username`) AS num FROM `users` WHERE `username`='".$username."'") or die(mysqli_error());
    $row = mysqli_fetch_assoc($data);
    $numUsers = $row['num'];
if ($numUsers >= 1){
        echo 'That user already exists.';
    }else{
        echo 'Username is available.';
    }

Upvotes: 0

Views: 291

Answers (1)

Marcel Korpel
Marcel Korpel

Reputation: 21763

There are other functions of the mysqli_result class that you should use, e.g. mysqli_fetch_assoc.

Said that, it's better to use prepared statements to bind and retrieve data, e.g.

$_stmt = $_mysqli->prepare('SELECT `permissies` FROM `gebruiker` WHERE (`id`=? AND `pwdenc`=?)');
$_stmt->bind_param('is', $_cred[0], $_cred[1]);
$_stmt->execute();
$_stmt->bind_result($perm);

Upvotes: 2

Related Questions