Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    99
    Thanks
    16
    Thanked 0 Times in 0 Posts

    converting to mysqli

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

    PHP Code:
    <?php
    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

    Paul

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,077
    Thanks
    2
    Thanked 320 Times in 312 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);
        list(
    $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);
        list(
    $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 12:02 AM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • Users who have thanked CFMaBiSmAd for this post:

    aquaman (07-03-2014)

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,341
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    The EASIEST way of converting is simply to use your text editor and replace
    mysql_query(

    with:
    mysqli_query($Link,

    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!
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    mysql_result doesn't have a mysqli equivalent so you have to create your own

    Code:
    function mysqli_result($Link, $row, $field=0) { 
        $Link->data_seek($row); 
        $datarow = $Link->fetch_array(); 
        return $datarow[$field]; 
    }
    Stephen
    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
    •