Jump to content
web_master

Pagination dont work with mysqli

Recommended Posts

Hi,

 

I got a problem with pagination. I got a script but the problem is that I use the connection to database with "mysqli" NOT "mysql". Im newbe with mysqli and I dont know how can I change the pagination script to work.

<?php
$dbConnect= mysqli_connect('localhost', 'user', 'password') or die('Cant connect');
mysqli_select_db($dbConnect, 'database') or die('Cant connect to database');
mysqli_set_charset($dbConnect, "utf8");
?>

And this is a script for pagination

<?php 
$limit = 4;
 
$sql = "select count(c_vesti_id) from c_vesti";

$c = array_shift(mysql_fetch_row(mysql_query($sql)));
 
$maxpage = ceil($c / $limit);
 
$page = isset($_GET['page']) ? abs((int)$_GET['page']) : 1;
if ($page <= 0)
{
        $page = 1;
}
else if ($page >= $maxpage)
{
        $page = $maxpage;
}
 
$offset = ($page-1) * $limit;
 
$query = mysql_query("select * from c_vesti limit $offset, $limit ");
 
while ($row = mysql_fetch_assoc($query))
{
        print $row['c_vesti_naslov']."<br />";
}
 
//
$linklimit = 4;
$linklimit2 = $linklimit / 2;
$linkoffset = ($page > $linklimit2) ? $page - $linklimit / 2 : 0;
$linkend = $linkoffset+$linklimit;
 
if ($maxpage - $linklimit2 < $page)
{
        $linkoffset = $maxpage - $linklimit;
        if ($linkoffset < 0)
        {
                $linkoffset = 0;
        }
        $linkend = $maxpage;
}
 
if ($page > 1)
{
        print "<a href='?page=".($page-1)."'>Back</a>   ";
}
for ($i=1+$linkoffset; $i <= $linkend and $i <= $maxpage; $i++)
{
        $style = ($i == $page) ? "color: black;" : "color: blue;";
        print "<a href='?page=$i' style='$style'>[$i]</a>   ";
}
if ($page < $maxpage)
{
        print "<a href='?page=".($page+1)."'>Forward</a>";
}
?>

Thanks in advanced for help

T

Share this post


Link to post
Share on other sites

Well if you're going to use mysqli then you would obviously need to use the mysqli functions to get info from the db too. Simply connecting with mysqli doesn't mean you can use the standard MySQL functions after that, umm like you are.

Share this post


Link to post
Share on other sites

Well if you're going to use mysqli then you would obviously need to use the mysqli functions to get info from the db too. Simply connecting with mysqli doesn't mean you can use the standard MySQL functions after that, umm like you are.

As I see there is a many ways to use mysqli in request data from database. I try simple to "rename" mysql_ to mysqli_ but it not work. Somewhere I need to connect to the database too, $dbConnect. This is a problem, how can I do this... :(

Share this post


Link to post
Share on other sites

 

try simple to "rename" mysql_ to mysqli_ but it not work.

You cannot just rename mysql_ functions over to mysqli_

 

Before renaming functions you should first consult the mysqli documentation, eg

http://php.net/mysqli_query ( concentrate on the documentation regarding the procedural method)

 

You will see at the top of the page, the function synopsis states the mysqli_query requires two arguments, the first being the mysqli instance (in your case $dbConnect) and the second being the query, so your use of mysqli_query would be like

$result = mysqli_query($dbConnect, $query);

And not just mysqli_query($query).

 

Looking at your pagination code this looks like this will be the only change you may need.

 

Generally the difference between the mysql and mysqli functions is where the mysql resource (connection) was optional in the mysql_ functions, now becomes required for the mysqli_ functions and is always the first argument.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.