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.

Community
  • 1
  • 1
MillerMedia
  • 3,651
  • 17
  • 71
  • 150
  • You are missing one parenthesis after `mysqli_query($query)`. – air4x Nov 03 '12 at 05:25
  • Ah yeah, just caught that. Doesn't really change the error I'm getting. I'll edit above. – MillerMedia Nov 03 '12 at 05:28
  • [`mysqli_query`](http://www.php.net/manual/en/mysqli.query.php) requires two arguments with the first one being the connection link, which is missing in your code – air4x Nov 03 '12 at 05: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? – MillerMedia Nov 03 '12 at 05: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.` – Asad Saeeduddin Nov 03 '12 at 05:48
  • "which I'm assuming is TRUE" Why would you assume that? Did you check? – Gavin Towey Nov 03 '12 at 09:51

3 Answers3

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";
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
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";
Vikas Umrao
  • 2,800
  • 1
  • 15
  • 23
  • 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. – MillerMedia Nov 03 '12 at 05:58
  • 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 ... – MillerMedia Nov 03 '12 at 06:09
  • You need to pass $conn as parameter in function pagination as i have edited above – Vikas Umrao Nov 03 '12 at 06:16
  • 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? – MillerMedia Nov 03 '12 at 06:39
  • 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'] – Vikas Umrao Nov 03 '12 at 06:49
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";
Vikas Umrao
  • 2,800
  • 1
  • 15
  • 23