2

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 an equivalent out there?

Here is my error

Fatal error: Call to undefined function mysqli_result() in C:\xampp\htdocs\bear_mysqli\functions\users.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;
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
hobbit
  • 59
  • 7

3 Answers3

1

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`
}
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • So, one-line query in php 7 is not works like it was in php 5 in mysql_result without prepared statements? `return (mysqli_result ($query, 0)== 1) ? true : false;` – Bogir Jan 20 '22 at 10:27
0

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.

Saqueib
  • 3,484
  • 3
  • 33
  • 56
-1

If you are trying to check if the username exists , you can do this :

$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";
}
Dana10
  • 61
  • 7
  • Regarding the question you just deleted, please understand that deleting questions consistently is not going to be helpful in all cases. In fact, deleting many questions can cause the site to rate-limit your further questions. – nanofarad Apr 19 '15 at 16:11
  • @hexafraction i'm new for the site , it's my mistake that i have not read the terms , i'm banned from asking questions , thank to you – Dana10 Apr 19 '15 at 16:15
  • I don't think you are banned at this point. Please just try to write a good and specific question, and then ask if, if you still need help. – nanofarad Apr 19 '15 at 16:16
  • @hexafraction the problem that i don't have no information for that app i searched the web and found nothing , maybe my reseach wasn't clear .. i will try again ... thank you :) – Dana10 Apr 19 '15 at 16:20