Jump to content

Whys it not working!


yandoo

Recommended Posts

Hi,

 

Im having a bit of trouble with my code. Bascally i have a query thta counts the number of total rows of records. LAter it is used as part of an If statement. Thing is when ever i echo out $totalrows i always get 1???? I shoud in fact have 21 as thats how many records i have.

 

 

$limit          = 4;             

    $query_count    = "SELECT count(*) FROM guest_book";   

    $result_count  = mysql_query($query_count);   

    $totalrows      = mysql_num_rows($result_count);

 

 

 

 

 

 

 

 

What am i doing wrong here???

Please help!!!

 

<?php
$dbservertype='mysql';
$servername='localhost';
// username and password to log onto db server
$dbusername='root';
$dbpassword='';
// name of database
$dbname='lopesarms';


connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
  error_reporting(E_ALL);
ini_set('display_errors', '1'); 

    $limit          = 4;               
    $query_count    = "SELECT count(*) FROM guest_book";    
    $result_count   = mysql_query($query_count);    
    $totalrows      = mysql_num_rows($result_count); 

    if(empty($page)){
        $page = 1;
    }
        

    $limitvalue = $page * $limit - ($limit); 
    $query  = "SELECT * FROM guest_book LIMIT $limitvalue, $limit";        
    $result = mysql_query($query) or die("Error: " . mysql_error()); 
echo"$totalrows";
    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    }

    $bgcolor = "#E0E0E0"; // light gray

    echo("<table>");
    
    while($row = mysql_fetch_array($result)){
        if ($bgcolor == "#E0E0E0"){
            $bgcolor = "#FFFFFF";
        }else{
            $bgcolor = "#E0E0E0";
        }

    echo("<tr bgcolor=".$bgcolor."><td>");
    echo($row["name"]);
    
    echo($row["email"]);
    echo("</td></tr>");
    }

    echo("</table>");

  echo"<font size=2>";
$pageprev = $page-1;

    if($page != 1){ 
       
        


  
        echo("<a href=\"$PHP_SELF?page=$pageprev\">PREV</a> "); 
    }else{
        echo("PREV"." ");
    }

    $numofpages =  $limit / $totalrows; 
    

    for($i = 1; $i <= $numofpages; $i++){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
        }
    }



    if(( $limit % $totalrows) != 0){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
        }
    }
$pagenext = $page+1;

//$new = ($limit * $page) - $totalrows;
//echo"$new";

    if(($limit * $page) - ($totalrows) > 0){
       
         



        echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT</a>");

    }else{
        echo("NEXT"); 

    }
    echo"</font>";
    mysql_free_result($result);

?>

 

Any help greatly appreciated :)

Link to comment
https://forums.phpfreaks.com/topic/68939-whys-it-not-working/
Share on other sites

Hi,

 

I tried adding in this line of code:

 

    $result = mysql_query($result_count) or die("Error: " . mysql_error());

 

 

 

is that right????

 

the message i got was:

 

 

Error: 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 'Resource id #3' at line 1

 

 

hhhmmm what does this mean???

 

Link to comment
https://forums.phpfreaks.com/topic/68939-whys-it-not-working/#findComment-346525
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.