PHP undefined mysqli_result

时间:2013-11-25 15:18:24

标签: php mysql

我正在检查数据库是否采用了用户名,但我不断收到此错误“在第9行调用未定义的函数mysqli_result()。

<?
$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.';
    }
  }
}
?>

也许是因为我使用jQuery处理来自不同文件的输入并用它来检查它,它说它是未定义的。这个相同的代码在我正在观看的教程中工作正常,但他使用的是mysql而不是mysqli。 编辑:固定代码看起来像替换$ username_query和$ 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.';
    }

1 个答案:

答案 0 :(得分:3)

您应该使用mysqli_result类的其他功能,例如mysqli_fetch_assoc

说,最好使用预准备语句来绑定和检索数据,例如

$_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);