SELECT COUNT (*) PAGINATION

Can anyone assist to point out what is wrong with line 8 code which resulted in the following error?

Parse error: syntax error, unexpected T_STRING in /home/xxxxxxxx/public_html/members/pagination.php on line 8

The PHP code in question is appended below:

1<?php
2
3 include(‘config.php’); // include your code to connect to DB.
4
5 $tbl_name = “products_attributes_download”; //your table name
6
7 $adjacents = 3;
8 $query = "SELECT COUNT () as num FROM $tbl_name" or die mysql_error() ;
9 $total_pages = mysql_fetch_array(mysql_query($query));
10 $total_pages = $total_pages[num];
11
12 /
Setup vars for query. */
13 $targetpage = “pagination.php”; //your file name (the name of this file)
14 $limit = 25; //how many items to show per page
15 $page = $_GET[‘page’];
16 if($page)
17 $start = ($page - 1) * $limit; //first item to display on this page
18 else
19 $start = 0;

Your help is appreciated.

try removing the space before the semi-colon.

Also structure your if-else statements in { and } to keep them organised.

Check line number 8 , where is mysql_query ?

Thanks RaythXC. I have removed the space between the semi-colon – NO LUCK.

Thanks sajan for the response.

However, by removing [or die mysql_error()], the following message appeared:

Connected SuccessfullyConnected to database successfully

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/xxxxxxxxxx/public_html/members/pagination.php on line 9

Your query (line 8) should be:

[php]
SELECT COUNT (*) FROM $tbl_name
[/php]

$query = "SELECT COUNT (*) as num FROM $tbl_name" or die mysql_error() ;

actually, sajan provided the correct answer. You have to perform the query.

$query = mysql_query("SELECT COUNT (*) as num FROM $tbl_name") or die mysql_error() ;

I tested RaythXC’s advice

$query = (SELECT COUNT (*) FROM $tbl_name);

But failed. The following message appeared.
Parse error: syntax error, unexpected T_STRING in /home/xxxxxxxxxx/public_html/members/pagination.php on line 8

I tried Laffin’s method

$query = mysql_query(“SELECT COUNT (*)as num FROM $tbl_name”) or die mysql_error();

It returned with the following message:
Parse error: syntax error, unexpected T_STRING in /home/xxxxxxxxxx/public_html/members/pagination.php on line 8

ok didnt see line 9

$query = "SELECT COUNT (*) as num FROM $tbl_name" or die mysql_error() ; $total_pages = mysql_fetch_array(mysql_query($query));
should be

$query = "SELECT COUNT (*) as num FROM $tbl_name"; $total_pages = mysql_fetch_array(mysql_query($query)) or die(mysql_error()) ;

That could have something to do with the fact theres no quote’s round the query.

Thanks Laffin for the code.
I have selected the given code below:

[php]$query = “SELECT COUNT (*) as num FROM $tbl_name”;
$total_pages = mysql_fetch_array(mysql_query($query)) or die(mysql_error()) ;
[/php]

and the result was:
Connected SuccessfullyConnected to database successfully)
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/xxxxxxxxxx/public_html/members/pagination.php on line 9

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘*) as num FROM products_attributes_download’ at line 1

What does this mean, please?

You can’t get an array from count () that’s why. Also try removing the space between COUNT and ()

Thanks RaythXC. Perfect. It works!

But followed with another error as below:

Connected SuccessfullyConnected to database successfully)
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/xxxxxxxxxx/public_html/members/pagination.php on line 119
? previous123456789next ?

118 <?php
119 while($row = mysql_fetch_array($result))
120 {
121 echo ‘

  • ’.$row[‘products_attributes_download’].’
  • ’;
    122 }
    123 ?>
    124
    125 <?=$pagination?>

    Thanks in advance for the help on resolving this error!

    Count returns a number so you can’t actually get an array from it.
    If you want to do this with just 1 query, simply do a normal SELECT * FROM table query, then use mysql_num_rows to figure out how many records rather than using COUNT

    Thanks RaythXC for the response.

    But I have to include below the image link that will be produced by the code. This is what I hope to see.

    [php]http://www.phpeasystep.com/phptu/29.html[/php]

    Below is the full code in question.
    [php]<?php

    include('config.php');	// include your code to connect to DB.
        
    $tbl_name = "products_attributes_download";		//your table name
    
    $adjacents = 3;
    $query = "SELECT COUNT(*) as num FROM $tbl_name";
        $total_pages = mysql_fetch_array(mysql_query($query))  or die(mysql_error()) ;
    $total_pages = $total_pages[num];
    
    /* Setup vars for query. */
    $targetpage = "pagination.php"; 	//your file name  (the name of this file)
    $limit = 25; 								//how many items to show per page
    $page = $_GET['page'];
    if($page) 
    
    	$start = ($page - 1) * $limit; 			//first item to display on this page
    
    else
    
    	$start = 0;								//if no page var is given, set start to 0
    
    /* Get data. */
    $sql = "SELECT column_name FROM $tbl_name LIMIT $start, $limit";
    $result = mysql_query($sql);
    
    /* Setup page vars for display. */
    if ($page == 0) $page = 1;					//if no page var is given, default to 1.
    $prev = $page - 1;							
    $next = $page + 1;							
    $lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
    $lpm1 = $lastpage - 1;			         //last page minus 1
    
    /* 
    	Now we apply our rules and draw the pagination object. 
    	We're actually saving the code to a variable in case we want to draw it more than once.
    */
    $pagination = "";
    if($lastpage > 1)
    {	
    	$pagination .= "<div class=\"pagination\">";
    	//previous button
    	if ($page > 1) 
    		$pagination.= "<a href=\"$targetpage?page=$prev\">? previous</a>";
    	else
    		$pagination.= "<span class=\"disabled\">? previous</span>";	
    	
    	//pages	
    	if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
    	{	
    		for ($counter = 1; $counter <= $lastpage; $counter++)
    		{
    			if ($counter == $page)
    				$pagination.= "<span class=\"current\">$counter</span>";
    			else
    				$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
    		}
    	}
    	elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
    	{
    		//close to beginning; only hide later pages
    		if($page < 1 + ($adjacents * 2))		
    		{
    			for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
    			{
    				if ($counter == $page)
    					$pagination.= "<span class=\"current\">$counter</span>";
    				else
    					$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
    			}
    			$pagination.= "...";
    			$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
    			$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";		
    		}
    		//in middle; hide some front and some back
    		elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
    		{
    			$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
    			$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
    			$pagination.= "...";
    			for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
    			{
    				if ($counter == $page)
    					$pagination.= "<span class=\"current\">$counter</span>";
    				else
    					$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
    			}
    			$pagination.= "...";
    			$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
    			$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";		
    		}
    		//close to end; only hide early pages
    		else
    		{
    			$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
    			$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
    			$pagination.= "...";
    			for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
    			{
    				if ($counter == $page)
    					$pagination.= "<span class=\"current\">$counter</span>";
    				else
    					$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
    			}
    		}
    	}
    	
    	//next button
    	if ($page < $counter - 1) 
    		$pagination.= "<a href=\"$targetpage?page=$next\">next ?</a>";
    	else
    		$pagination.= "<span class=\"disabled\">next ?</span>";
    	$pagination.= "</div>\n";		
    }
    

    ?>

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

    [/php]

    Thanks from the bottom of my heart for the effort in helping to sort out this problem!

    By the way this issue is STILL UNSOLVED. I hope to see the outcome similar to the image in the link given above.

    Sponsor our Newsletter | Privacy Policy | Terms of Service