Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't Change mysql_fetch_array to mysqli_fetch_array
    text
    copied!<p>I'm working on pagination on a website I'm building and I found a good pagination example here: <a href="http://www.awcore.com/dev/1/3/Create-Awesome-PHPMYSQL-Pagination_en" rel="nofollow noreferrer">http://www.awcore.com/dev/1/3/Create-Awesome-PHPMYSQL-Pagination_en</a> . I'd like to us this on my page but am using mysqli instead of mysql and need to convert some of it.</p> <p>I'm new to MySQL and am trying to figure out the syntax to converting the following code:</p> <pre><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"; </code></pre> <p>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:</p> <pre><code>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"; </code></pre> <p>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.</p> <p>BTW I did see this question (<a href="https://stackoverflow.com/questions/10518090/converting-from-mysql-to-mysqli-mysql-fetch-array">Converting from mysql to mysqli (mysql_fetch_array)</a>) but it seemed like he was taking several extra steps that may not need to be taken.</p> <h2>EDIT</h2> <p>Here's the error messages I'm getting with the above code FYI:</p> <blockquote> <p>Warning: mysqli_query() expects at least 2 parameters, 1 given in linkinformation/functions.php on line 9</p> <p>Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in linkinformation/functions.php on line 9</p> </blockquote> <h2>EDIT</h2> <p>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):</p> <pre><code>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"; </code></pre> <p>I get this warning:</p> <pre><code>Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in linkstuff/functions.php on line 10 </code></pre> <p>It's getting a boolean (which I'm assuming is TRUE) whereas it should be getting something else I suppose/the actual query.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload