简体   繁体   中英

Replace mysql_result with mysqli_?

I've have an issue with replacing the mysql_result to the correct mysqli_? command. It feels like I've tried everything but nothing seems to be working. I'm using it to check if a username is taken for a login-system, like this:

   $sql = "SELECT COUNT(*) FROM members WHERE user='{$_POST['user']}'";
   $result = mysqli_query($con, $sql);
   if (mysql_result($result, 0) > 0) {
     $reg_error[] = 1;
   }

I've tried with mysqli_fetch_assoc and mysqli_num_rows and I've tried to change the whole thing but nothing is working so I'm turning to you guys.

With everything I've tried my script still creates a user even if the username allready exists.

Is there anyway to solve this problem?

This is what I use to check if a username exists.

$user= mysqli_real_escape_string($con, $_POST['user']);

$result = mysqli_query($con, "SELECT * FROM `members` WHERE `user`='$user'");

if ($result->num_rows)
  {
echo "ID exist!";
} 
  else
   {
     echo "ID doesnt exist!"
   }

and/or:

$user= mysqli_real_escape_string($con, $_POST['user']);

$query = mysqli_query($con, "SELECT * FROM `members` WHERE `user`='".$user."'");

if(mysqli_num_rows($query) > 0){

    echo "email already exists";
}else{

// do something here

}

Maybe you can try

$result = mysqli_query($con,$sql); 
$row = mysqli_fetch_array($result);

Then check the value in $row

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM