我有一个旧代码,它使用mysql作为其扩展来与MySQL通信。问题是,我找不到等价的mysql_result
来将此代码转换为mysqli。
正如您所见,SQL代码将执行并检查结果是否为0或1及以上。 (如果为0,则为false。如果大于1,则为真)
if (empty($_POST)===false) {
$username = $_POST['username'];
$password = $_POST['password'];
$me = new me($username, $password);
if (empty($username)===true||empty($password)===true) {
die("error1");
} else if ($me->checkexist===false) {
die("error2");
} else {echo "true";}
}
在类文件中:
class me {
public $uname;
public $pswd;
public function __construct($uname, $pswd) {
$this->username = $uname;
$this->password = $pswd;
}
public function checkexist() {
$sql = "SELECT COUNT(`uid`) FROM `users` WHERE `username` = '$this->username'";
$result = $conn->mysqli_query($sql);
return ($result->fetch_array()[0] == 1) ? true : false;
}
}