yandoo Posted September 12, 2007 Share Posted September 12, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/68939-whys-it-not-working/ Share on other sites More sharing options...
cooldude832 Posted September 12, 2007 Share Posted September 12, 2007 don't echo a variable in quotes, also run an or die on that query for the count to see if its working right Quote Link to comment https://forums.phpfreaks.com/topic/68939-whys-it-not-working/#findComment-346523 Share on other sites More sharing options...
yandoo Posted September 12, 2007 Author Share Posted September 12, 2007 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??? Quote Link to comment https://forums.phpfreaks.com/topic/68939-whys-it-not-working/#findComment-346525 Share on other sites More sharing options...
cooldude832 Posted September 12, 2007 Share Posted September 12, 2007 $result_count = $query_count and you don't need to add a query just add the or die(mysql_error()); portion to the query execution Quote Link to comment https://forums.phpfreaks.com/topic/68939-whys-it-not-working/#findComment-346527 Share on other sites More sharing options...
yandoo Posted September 12, 2007 Author Share Posted September 12, 2007 Ive have just tried using only the or die bit as suggested and the error message has gone but the $totalrows still only 1?? whats u think? Quote Link to comment https://forums.phpfreaks.com/topic/68939-whys-it-not-working/#findComment-346531 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.