Mysql to Mysqli

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Mysql to Mysqli

Post by Epiales »

GRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR

Okay, I've done well with my mysqli... so far everything I've done has it. But I borrowed the pagination system from online and it's in mysql. I have spent several HOURS trying to convert it. Can someone take a look and see if they can? I'll be working on it still, but there's only a few lines that need to be changed and I can't figure it out yet. Thank you if you have time.... If not, I'll get it eventually, as I always do, but would be helpful to get her done so I can move on :)

Does it really matter on this part? Since it's only going to be used for members list and rankings? Or should I go ahead and convert to Mysqli?

Code: Select all

<?php
//********************BEGINNING OF THE MEMBERS PAGINATION PHP CODING ********************//
if(isset($_GET['members'])) {

$bypass = 1;


mysql_connect("localhost","root","") or die (mysql_error());
mysql_select_db("login2") or die (mysql_error());

$sql = mysql_query("SELECT id, username, country FROM users ORDER BY id ASC");

$nr = mysql_num_rows($sql);  
if (isset($_GET['pn'])) {  
    $pn = preg_replace('#[^0-9]#i', '', $_GET['pn']);  

} else { 
    $pn = 1;
} 

$itemsPerPage = 10; 

$lastPage = ceil($nr / $itemsPerPage);

if ($pn < 1) {  
    $pn = 1;  
} else if ($pn > $lastPage) {  
    $pn = $lastPage;  
} 


$centerPages = "";
$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;
if ($pn == 1) {
    $centerPages .= '  <span class="pagNumActive">' . $pn . '</span>  ';
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?members=1&pn=' . $add1 . '">' . $add1 . '</a>  ';
} else if ($pn == $lastPage) {
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?members=1&pn=' . $sub1 . '">' . $sub1 . '</a>  ';
    $centerPages .= '  <span class="pagNumActive">' . $pn . '</span>  ';
} else if ($pn > 2 && $pn < ($lastPage - 1)) {
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?members=1&pn=' . $sub2 . '">' . $sub2 . '</a>  ';
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?members=1&pn=' . $sub1 . '">' . $sub1 . '</a>  ';
    $centerPages .= '  <span class="pagNumActive">' . $pn . '</span>  ';
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?members=1&pn=' . $add1 . '">' . $add1 . '</a>  ';
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?members=1&pn=' . $add2 . '">' . $add2 . '</a>  ';
} else if ($pn > 1 && $pn < $lastPage) {
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?members=1&pn=' . $sub1 . '">' . $sub1 . '</a>  ';
    $centerPages .= '  <span class="pagNumActive">' . $pn . '</span>  ';
    $centerPages .= '  <a href="' . $_SERVER['PHP_SELF'] . '?members=1&pn=' . $add1 . '">' . $add1 . '</a>  ';
}

$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage; 


$sql2 = mysql_query("SELECT id, username, country FROM users ORDER BY id ASC $limit"); 

$paginationDisplay = "";  

if ($lastPage != "1"){

    $paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '        ';

    if ($pn != 1) {
        $previous = $pn - 1;
        $paginationDisplay .=  '   <a href="' . $_SERVER['PHP_SELF'] . '?members=1&pn=' . $previous . '"> Back</a> ';
    } 

    $paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';

    if ($pn != $lastPage) {
        $nextPage = $pn + 1;
        $paginationDisplay .=  '   <a href="' . $_SERVER['PHP_SELF'] . '?members=1&pn=' . $nextPage . '"> Next</a> ';
    } 
}

$outputList = '';
while($row = mysql_fetch_array($sql2)){ 

    $id = $row["id"];
    $firstname = $row["username"];
    $country = $row["country"];

    $outputList .= '<b><a href=\'user.php?u=' .$firstname.'\'style="text-decoration: none; color:#000000">' . $firstname . '</a></b><hr />';
           
    } 
}

?>
<html>
<head>
<style type="text/css">
<!--
.pagNumActive {
    color: #fff;
    border:#060 1px solid; background-color: #660000; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:link {
    color: #000;
    text-decoration: none;
    border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:visited {
    color: #000;
    text-decoration: none;
    border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:hover {
    color: #000;
    text-decoration: none;
    border:#060 1px solid; background-color: #D2FFD2; padding-left:3px; padding-right:3px;
}
.paginationNumbers a:active {
    color: #000;
    text-decoration: none;
    border:#999 1px solid; background-color:#F0F0F0; padding-left:3px; padding-right:3px;
}
-->
</style>
<?php

if ($bypass != 0) {


?>
</head>
<body>
   <div style="margin-left:64px; margin-right:64px;">
     <h2>Total Members: <?php echo $nr; ?></h2><br />
     <h3>Click members to view profile</h3>
   </div> 
      <div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>
      <div style="margin-left:64px; margin-right:64px;"><?php echo "$outputList"; ?></div>
      <div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>
</body>
</html>
<?PHP
exit;
//********************ENDING OF THE MEMBERS PAGINATION PHP CODING ********************//
Nothing fancy, but a work in progress!

http://gameplaytoday.net
sniko
Posts: 23
Joined: Fri Jan 17, 2014 2:56 pm

Re: Mysql to Mysqli

Post by sniko »

Should you convert it to use mysqli api rather than mysql? Yes. Vanilla mysql_* functions are deprecated and will be dropped, therefore once your hosting provider (or you) upgrade your PHP version somewhere in the near future, your application won't work anymore.

As for converting, can you show us what you've tried? And are you heading down the procedural or object oriented approach with mysqli?
Image

Code: Select all

class Life extends Bitch {
  //...
}
Post Reply

Return to “Beginner Help and Support”