Can't Change mysql_fetch_array to mysqli_fetch_array

10,527

You need to change your connection to mysqli as well, and pass the connection object to mysqli_query as the first argument.

function pagination($link, $query, $per_page = 10,$page = 1, $url = '?'){        
    $query = "SELECT COUNT(*) as `num` FROM {$query}";
    $row = mysqli_fetch_array(mysqli_query($link, $query));
    $total = $row['num'];
    $adjacents = "2";
Share:
10,527
MillerMedia
Author by

MillerMedia

Thanks for the help! Any programmers looking for subcontractor work, feel free to e-mail me at [email protected] . We're always looking for great workers. Thanks!

Updated on June 04, 2022

Comments

  • MillerMedia
    MillerMedia almost 2 years

    I'm working on pagination on a website I'm building and I found a good pagination example here: http://www.awcore.com/dev/1/3/Create-Awesome-PHPMYSQL-Pagination_en . I'd like to us this on my page but am using mysqli instead of mysql and need to convert some of it.

    I'm new to MySQL and am trying to figure out the syntax to converting the following code:

    function pagination($query, $per_page = 10,$page = 1, $url = '?'){        
        $query = "SELECT COUNT(*) as `num` FROM {$query}";
        $row = mysql_fetch_array(mysql_query($query));
        $total = $row['num'];
        $adjacents = "2"; 
    

    I know it doesn't give you much as to what the entire code is (it's about 100 lines long) but I'm assuming this may be an easy syntax change. I had initially done it likes this:

    function pagination($query, $per_page = 10,$page = 1, $url = '?'){        
        $query = "SELECT COUNT(*) as `num` FROM {$query}";
        $row = mysqli_fetch_array(mysqli_query($query));
        $total = $row['num'];
        $adjacents = "2";
    

    I am aware that mysqli_query needs to take two parameters but I was also under the impression that it was the same for mysql_query so I guess I'm just not understanding the code. Sorry if this is a super basic question, I'm just trying to wrap my head around some of these concepts! Thanks for any help.

    BTW I did see this question (Converting from mysql to mysqli (mysql_fetch_array)) but it seemed like he was taking several extra steps that may not need to be taken.

    EDIT

    Here's the error messages I'm getting with the above code FYI:

    Warning: mysqli_query() expects at least 2 parameters, 1 given in linkinformation/functions.php on line 9

    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in linkinformation/functions.php on line 9

    EDIT

    So I added a connection within the function (is that the right approach? I tried connecting outside the function but it was grabbing the info):

    function pagination($mysqli, $query, $per_page = 10,$page = 1, $url = '?'){        
        $mysqli = mysqli_connect("localhost","username","password", "db_name");
        $query = "SELECT COUNT(*) as `num` FROM {$query}";
        $row = mysqli_fetch_array(mysqli_query($mysqli, $query));
        $total = $row['num'];
        $adjacents = "2"; 
    

    I get this warning:

    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in linkstuff/functions.php on line 10
    

    It's getting a boolean (which I'm assuming is TRUE) whereas it should be getting something else I suppose/the actual query.