Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2012
    Thanked 0 Times in 0 Posts

    converting to mysqli

    Hi, I am having real trouble converting this to the newer MySQLi code

    PHP Code:
    function user_exists($username) {
        return (
    mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'"), 0) == 1) ? true false;
    I tried changing the mysql_query to mysqli_query and the same for result. I have tried several ways but can't seem to get it to run properly. The function is called into another page which contains the mysqli login details.

    Does anybody know how to change the above code so it works? I have got several functions like this and am getting nowhere

    Many Thanks


  2. #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Denver, Colorado USA
    Thanked 510 Times in 498 Posts
    1) mysqli_query() requires the connection resource as a parameter, so at a minimum, you must pass the variable holding your mysqli connection into the function, both in the function definition and everywhere that you call the function.

    2) You should form your sql query statement in a php variable, this helps both for debugging and in this case would have separated it from the code running the query making it clearer and easier to convert to mysqli_ functions.

    3) You should never nest functions that can fail, the way that code is written. You cannot debug when the query is failing due to an error. There's no point in writing code all on one line if you cannot read it or when the time comes, make changes to it.

    4) There is no mysqli_result() function, probably because the way it works is so inefficient. You could write your own function that emulates what mysql_query does if you want.

    How that function code SHOULD have been written
    PHP Code:
    function user_exists($username) {
    $query "SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'";
    $result mysql_query($query);
    $count) = mysql_fetch_row($result);
        return (
    $count == 1) ? true false;

    How you would write it using msyqli_ functions, where the $db parameter is your mysqli connection resource
    PHP Code:
    function user_exists($db$username) {
    $query "SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username'";
    $result mysqli_query($db,$query);
    $count) = mysqli_fetch_row($result);
        return (
    $count == 1) ? true false;

    The reason for putting the $db parameter as the first one is to follow the convention established by the msyqli_ functions themselves, i.e. it's required, whereas you may have optional parameters later in the list, so it is put first.
    Last edited by CFMaBiSmAd; 07-01-2014 at 01:02 AM.
    Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

  3. Users who have thanked CFMaBiSmAd for this post:

    aquaman (07-03-2014)

  4. #3
    Master Coder
    Join Date
    Feb 2011
    Your Monitor
    Thanked 615 Times in 601 Posts
    The EASIEST way of converting is simply to use your text editor and replace


    Then all you have to do is initialise the $Link variable when you connect to your database. Any functions that access the DB will need the $Link var passed in as a parameter, or $Link to be a global that they can access.

    Job done!

    As CFM says, you should put your queries into a variable too because then you can print them to the screen, copy, paste into phpmyadmin and test them. Unfortunately though if you use prepared statements with bound variables this doesn't work - but at least you can still see the finished query if you need to!
    Quote Originally Posted by deathshadow View Post
    So seriously, loosen up that tie, let out the belt, and try relating to normal people on the street instead of the gentleman's club crowd.

  5. #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Sydney, Australia
    Thanked 932 Times in 919 Posts
    mysql_result doesn't have a mysqli equivalent so you have to create your own

    function mysqli_result($Link, $row, $field=0) { 
        $datarow = $Link->fetch_array(); 
        return $datarow[$field]; 
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts