php4 to php5 conversion help

Hi
I am trying to revive an old script but I am having troubles with 2 sections.
It is an old pagination script (which i really need).
The script gives me errors in 2 sections. section 1:
[php]<?php
//show records
$query = mysql_query(‘SELECT * FROM {$statement} LIMIT {$startpoint} , {$limit}’);

    	while ($row = mysql_fetch_assoc($query)) {
    ?>
        <div class="record round"><?php echo '{$row["userID"]}#{$row["username"]}';?></div>
    <?php    
        }
    ?>[/php]

The line [php]while ($row = mysql_fetch_assoc($query)) {[/php]

gives me a warning: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\page.php on line 101

The second part of the script:
[php] 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”; [/php]

The line: [php]$row = mysql_fetch_array(mysql_query($query));[/php]

Gives me a warning: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\function.php on line 9

What do I need to do to get this updated to php5? This script used to work perfectly a few years back until php5 arrived. thanks

few years back until php5 arrived.
A few as in 11; PHP4 stopped being supported about 7 years ago, not a small amount of time, especially in development years.

There is a lot of differences, for one, you should no longer be using mysql_ functions on databases. You should be using PDO or mysqli.

am i wasting my time trying to convert this? if so i will move on and try to find one already made on the internet. it is a shame because this worked fine and i rather liked the way it worked and looked. i know its a been some years but i did say it was old ;D If it is possible to convert then i would rather give it a try

It’s not a waste of time just as long as you learn the new way of getting data in and out of a database using mysqli or PDO. There is nothing wrong learning mysqli, but in my opinion learning PDO is the better of the two options. First, you can use more that one database type, whereas with mysqli you are tied down to just MySQL. Second, I have seen people posting for help on mysqli that they think all you need to do is put an i at the end of mysql…nope. Thirdly and lastly while PDO might be a little harder to learn, I think it’s more intuitive than mysql. Just my .02 cents.

Just use PDO. There, it’s settled. To get you started you can check out the link to my PDO Bumpstart Database in my signature.

thanks for your replies but ive dumped this script and going to try a new one. i will start a new topic as it is a different script. thanks again

Hi, back again with a different script which i would really like to get working in php5. I seem to have two choices either get this script converted to php5 or install an older version of php on my server but i really hope it doesnt come to that ;D

this is the main script that contains all the old php4 stuff but to be honest there seems to be only 3 or 4 parts that need converting. I will provide a link to the output page in case it is needed. this is the script below as it was before I made any edits.

[php]

Papermashup.com | PHP Pagination .paginate { font-family:Arial, Helvetica, sans-serif; padding: 3px; margin: 3px; } .paginate a { padding:2px 5px 2px 5px; margin:2px; border:1px solid #999; text-decoration:none; color: #666; } .paginate a:hover, .paginate a:active { border: 1px solid #999; color: #000; } .paginate span.current { margin: 2px; padding: 2px 5px 2px 5px; border: 1px solid #999; font-weight: bold; background-color: #999; color: #FFF; } .paginate span.disabled { padding:2px 5px 2px 5px; margin:2px; border:1px solid #eee; color:#DDD; } li{ padding:4px; margin-bottom:3px; background-color:#FCC; list-style:none;} ul{margin:6px; padding:0px;} <?php <?php require('includes/config.php'); ?>
$tableName="members";		
$targetpage = "pagination.php"; 	
$limit = 10; 

$query = "SELECT COUNT(*) as num FROM $tableName";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
	$start = ($page - 1) * $limit; 
}else{
	$start = 0;	
	}	

// Get page data
$query1 = "SELECT * FROM $tableName LIMIT $start, $limit";
$result = mysql_query($query1);

// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;	
$next = $page + 1;							
$lastpage = ceil($total_pages/$limit);		
$LastPagem1 = $lastpage - 1;					


$paginate = '';
if($lastpage > 1)
{	




	$paginate .= "<div class='paginate'>";
	// Previous
	if ($page > 1){
		$paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
	}else{
		$paginate.= "<span class='disabled'>previous</span>";	}
		

	
	// Pages	
	if ($lastpage < 7 + ($stages * 2))	// Not enough pages to breaking it up
	{	
		for ($counter = 1; $counter <= $lastpage; $counter++)
		{
			if ($counter == $page){
				$paginate.= "<span class='current'>$counter</span>";
			}else{
				$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
		}
	}
	elseif($lastpage > 5 + ($stages * 2))	// Enough pages to hide a few?
	{
		// Beginning only hide later pages
		if($page < 1 + ($stages * 2))		
		{
			for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
			{
				if ($counter == $page){
					$paginate.= "<span class='current'>$counter</span>";
				}else{
					$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
			}
			$paginate.= "...";
			$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
			$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";		
		}
		// Middle hide some front and some back
		elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
		{
			$paginate.= "<a href='$targetpage?page=1'>1</a>";
			$paginate.= "<a href='$targetpage?page=2'>2</a>";
			$paginate.= "...";
			for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
			{
				if ($counter == $page){
					$paginate.= "<span class='current'>$counter</span>";
				}else{
					$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
			}
			$paginate.= "...";
			$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
			$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";		
		}
		// End only hide early pages
		else
		{
			$paginate.= "<a href='$targetpage?page=1'>1</a>";
			$paginate.= "<a href='$targetpage?page=2'>2</a>";
			$paginate.= "...";
			for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
			{
				if ($counter == $page){
					$paginate.= "<span class='current'>$counter</span>";
				}else{
					$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
			}
		}
	}
				
			// Next
	if ($page < $counter - 1){ 
		$paginate.= "<a href='$targetpage?page=$next'>next</a>";
	}else{
		$paginate.= "<span class='disabled'>next</span>";
		}
		
	$paginate.= "</div>";		

}
echo $total_pages.’ Results’;
// pagination
echo $paginate;
?>

    <?php while($row = mysql_fetch_array($result)) { echo '
  • '.$row['country'].'
  • '; } ?>
[/php]

from line 66 i converted it to this which i think is right (i get no errors from it anyway)
[php] $stmt = $db->prepare(“SELECT COUNT(*) as num FROM $tableName”);
$stmt->bindParam(’:$tableName’, $q, PDO::PARAM_INT);
$stmt->execute();
$total_pages = $stmt->fetchColumn(0);[/php]

the other parts which use mysql_ is where i have all the troubles. I don’t know php5 so I need help with come conversions, I think I can manage the other parts of the script :slight_smile:

This is the page where i am testing it out…
http://www.finchkeeper.com/pagination.php

you can’t use placeholders for table, column names or other parts of the actual query. Only data to be queried for / inserted.

In your script “members” is hard coded so there’s no real reason to have it in a variable.

"SELECT COUNT(*) as num FROM members"

I understand about the variable but that was how it was when i got it, it is easily changed :slight_smile:
I am trying to learn the new php5 but i do have a long way to go. my very first attempt was this part below.
[php] $stmt = $db->prepare(“SELECT COUNT(*) as num FROM $tableName”);
$stmt->bindParam(’:$tableName’, $q, PDO::PARAM_INT);
$stmt->execute();
$total_pages = $stmt->fetchColumn(0);[/php]

although i get no errors from that i am not sure if it is doing what it is meant to do. it does return 4 results the same as the amount of members in the user db.

You are passing in the table name by variable, bypassing the actual prepared statement, which is why it is working.
But, you should be getting a parameter mismatch count error. PDO error reporting would need to be turned on, for development, as well.

its ok now i finally got the whole script working perfectly. below is the whole script but only missing the config.php file. I hope someone else in future can make some use of this script because i spent days on the internet looking for such a script in php5 or higher and found nothing but crappy simple ones. here is the finished scrip.
[PHP]

Papermashup.com | PHP Pagination .paginate { font-family:Arial, Helvetica, sans-serif; padding: 3px; margin: 3px; }

.paginate a {
padding:2px 5px 2px 5px;
margin:2px;
border:1px solid #999;
text-decoration:none;
color: #666;
}

.paginate a:hover, .paginate a:active {
border: 1px solid #999;
color: #000;
}

.paginate span.current {
margin: 2px;
padding: 2px 5px 2px 5px;
border: 1px solid #999;
font-weight: bold;
background-color: #999;
color: #FFF;
}

.paginate span.disabled {
padding:2px 5px 2px 5px;
margin:2px;
border:1px solid #eee;
color:#DDD;
}

li {
padding:4px;
margin-bottom:3px;
background-color:#FCC;
list-style:none;
}

ul {
margin:6px;
padding:0px;
}


<?php require('includes/config.php'); $targetpage = 'pagination.php'; $limit = 3;
$stmt = $db->prepare("SELECT COUNT(*) as num FROM members");
$stmt->bindParam(':members', $q, PDO::PARAM_INT);
$stmt->execute();
$total_pages = $stmt->fetchColumn(0);

$stages = 3;
$page = (isset($_GET["page"])) ? (int)$_GET["page"] : 1;
if($page){
	$start = ($page - 1) * $limit; 
}else{
	$start = 0;	
	}	

$sql = “SELECT * FROM members LIMIT $start, $limit”;
foreach ($db->query($sql) as $row) {
}
// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;
$paginate = ‘’;
if($lastpage > 1)
{
$paginate .= “

”;
// Previous
if ($page > 1){
$paginate.= “previous”;
}else{
$paginate.= “previous”; }
// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= “$counter”;
}else{
$paginate.= “$counter”;}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= “$counter”;
}else{
$paginate.= “$counter”;}
}
$paginate.= “…”;
$paginate.= “$LastPagem1”;
$paginate.= “$lastpage”;
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= “1”;
$paginate.= “2”;
$paginate.= “…”;
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= “$counter”;
}else{
$paginate.= “$counter”;}
}
$paginate.= “…”;
$paginate.= “$LastPagem1”;
$paginate.= “$lastpage”;
}
// End only hide early pages
else
{
$paginate.= “1”;
$paginate.= “2”;
$paginate.= “…”;
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= “$counter”;
}else{
$paginate.= “$counter”;}
}
}
}
// Next
if ($page < $counter - 1){
$paginate.= “next”;
}else{
$paginate.= “next”;
}
$paginate.= “
”;
}
// pagination
echo $paginate;
?>
    <?php foreach ($db->query($sql) as $row) { echo '
  • '.$row['username'].' '.$row['urank'].'
  • ';
	}

echo $paginate;
?>

[/PHP]

I really appreciate all the replies and help but i ended up getting a little help from a php facebook group, thanks again

Sponsor our Newsletter | Privacy Policy | Terms of Service