2

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.

6
  • You are missing one parenthesis after mysqli_query($query). Commented Nov 3, 2012 at 5:25
  • Ah yeah, just caught that. Doesn't really change the error I'm getting. I'll edit above. Commented Nov 3, 2012 at 5:28
  • mysqli_query requires two arguments with the first one being the connection link, which is missing in your code Commented Nov 3, 2012 at 5:33
  • I'm curious how it would have functioned properly in the original code written for mysql_query? Did that only have to take one parameter? Commented Nov 3, 2012 at 5:42
  • From the docs for mysql_query: If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated. Commented Nov 3, 2012 at 5:48

3 Answers 3

2

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";
Sign up to request clarification or add additional context in comments.

Comments

0

Something like this:

//You need to change thses variables    
$conn = new mysqli('localhost','root','',$databasename);

    function pagination( $conn,$query, $per_page = 10,$page = 1, $url = '?'){        
        $query = "SELECT COUNT(*) as `num` FROM {$query}";
        $result = $conn->query($query);
        $row=$result->fetch_array;
        $total = $result->num_rows;
        $adjacents = "2";

5 Comments

I got this error with that: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_STRING or T_VARIABLE or '{' or '$' in /linkstuff/functions.php on line 7. Line 7 is the line starting with $conn btw.
The curly bracket doesn't need to be there necessarily because it's part of a much bigger function but I see what you're saying. This code is still giving me an error. This time it's: Fatal error: Call to a member function query() on a non-object in linkstuff/functions.php on line 11. Line 11 is the line starting with $result ...
You need to pass $conn as parameter in function pagination as i have edited above
I'm thinking the new mysqli connection ($conn) would have to be created within the function if you're using it as a parameter, correct?
it will be better to define $conn as global $conn at top of page and then you do not need to pass it as parameter. you can access it as $GLOBAL['conn']
0

//You need to change thses variables

global $conn;  
$conn = new mysqli('localhost','root','',$databasename);

    function pagination($query, $per_page = 10,$page = 1, $url = '?'){        
        $query = "SELECT COUNT(*) as `num` FROM {$query}";
        $result = $GLOBALS['conn']->query($query);
        $row=$result->fetch_array;
        $total = $result->num_rows;
        $adjacents = "2";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.