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.

Mysql to Mysqli

Postby Epiales » Fri Sep 26, 2014 2:11 pm

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

$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'&nbsp;  &nbsp;  &nbsp; ';

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

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

    if (
$pn != $lastPage) {
        
$nextPage $pn 1;
        
$paginationDisplay .=  '&nbsp;  <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
User avatar
Epiales
 
Posts: 1119
Joined: Wed Aug 14, 2013 8:38 pm
Has thanked: 62 times
Been thanked: 6 times

Re: Mysql to Mysqli

Postby sniko » Sat Oct 18, 2014 12:15 pm

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 {
  //...
}
sniko
 
Posts: 23
Joined: Fri Jan 17, 2014 9:56 am
Has thanked: 0 time
Been thanked: 6 times


Return to Beginner Help and Support

Who is online

Users browsing this forum: No registered users and 1 guest

x