result not working converting mysql to mysqli
i am converting my old mysql to mysqli but ran into this problem mysqli result does not work.looked on the internet and i founded out that it function had been change,for mysqli result is there a equivalent out here?
thank
here my error
Fatal error: Call to undefined function mysqli_result() in C:xampphtdocsbear_mysqlifunctionsusers.php on line 153
function user_exists($con,$username) { $username = sanitize($con,$username); $query = mysqli_query($con,"SELECT COUNT(`user_id`)FROM `users` WHERE `username` = '$username'"); return (mysqli_result ($query, 0)== 1) ? true : false; }
No, there is no mysql_result
to mysqli_result
counter-part. If you're just checking if it exists, use just ->num_rows
:
And use prepared statements instead.
function user_exists($con, $username) {
$sql = 'SELECT * FROM `users` WHERE `username` = ?';
$select = $con->prepare($sql);
$select->bind_param('s', $username);
$select->execute();
$select->store_result();
return ($select->num_rows > 0); // returns bool (true/false)
}
Or use an alias for your COUNT()
and fetch it:
function user_exists($con, $username) {
$sql = 'SELECT COUNT(`user_id`) AS `total` FROM `users` WHERE `username` = ?';
$select = $con->prepare($sql);
$select->bind_param('s', $username);
$select->execute();
$select->store_result();
$select->bind_result($total);
$select->fetch();
return $total; // returns the result yielded by alias `total`
}
Enable mysqli
in php.ini
Search extension=php_mysqli.dll
and uncomment in php.ini
just remove ;
before extension=php_mysqli.dll
; Windows Extensions
; Note that ODBC support is built in, so no dll is needed for it.
; Note that many DLL files are located in the extensions/ (PHP 4) ext/ (PHP 5)
; extension folders as well as the separate PECL DLL download (PHP 5).
; Be sure to appropriately set the extension_dir directive.
extension=php_mysqli.dll
and Restart your xampp once you have applied the settings.
如果你想检查用户名是否存在,你可以这样做:
$urexist="SELECT * FROM persons WHERE Email = '$_POST[EMailTxt]'";
$res = mysqli_query($conect,$urexist);//you should add a connection paramenter
$data = mysqli_fetch_array($res, MYSQLI_NUM);
if($data[0] > 1) {
echo "User Exists";
}
链接地址: http://www.djcxy.com/p/69534.html
上一篇: PHP无法重新声明函数
下一篇: 结果不能将mysql转换为mysqli