hmm, works but doesnt...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Monotoko
Forum Commoner
Posts: 64
Joined: Fri Oct 26, 2007 4:24 pm

hmm, works but doesnt...

Post by Monotoko »

Well, the following code is meant to paginate what i get from the database:

Code: Select all

<?
 
$con = mysql_connect("localhost","xxxxxxx_admin","xxxxxxxxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("gamersne_mysys", $con);
 
echo "<table class='tborder' cellpadding='6' cellspacing='1' border='0' width='100%'
style='border-bottom-width:0px'>
<tr>
    <td class='tcat' width='100%'>
        <div align='center'>Members List</div></td>
    
</tr>
</table>
<table class='tborder' cellpadding='6' cellspacing='1' border='0' width='100%' align='center'>
<tr>
    <td class='alt1'><p align='center'><table border='1'>
<tr>
<td>Member</td>
<td>Gamers Level</td>
<td>Group</td>
<td>Clan</td>
<td>Joined</td>
<td>Posts</td>
<td>email</td>
</tr>
<tr>";
 
if (isset($_GET['pageno'])) 
{
 $pageno = $_GET['pageno'];
} 
else
{
 $pageno = 1;
}
 
$result = mysql_query("SELECT * FROM members");
$query_data = mysql_num_rows($result);
$numrows = $query_data[0];
$rows_per_page = 15;
$lastpage      = ceil($numrows/$rows_per_page);
$pageno = (int)$pageno;
if ($pageno < 1) {
   $pageno = 1;
} elseif ($pageno > $lastpage) {
   $pageno = $lastpage;
}
$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
$query = "SELECT * FROM members $limit";
$result = mysql_query($query, $con);
$name = $row['username'];
$lvl = $row['level'];
$group = $row['group'];
$email = $row['email'];
$clan = $row['clan'];
$posts = $row['posts'];
$joined = $row['joined'];
 
echo "<tr>
<td>$name</td>
<td>$lvl</td>
<td>$clan</td>
<td>$group</td>
<td>$joined</td>
<td>$posts</td>
<td>$email</td>
</tr></table>";
 
if ($pageno == 1) {
   echo " FIRST PREV ";
} else {
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
   $prevpage = $pageno-1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
}
 
echo " ( Page $pageno of $lastpage ) ";
 
if ($pageno == $lastpage) {
   echo " NEXT LAST ";
} else {
   $nextpage = $pageno+1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
}
?>
Now, all seems to be well, except it is not getting the data from my database at all :S
The table is just blank, it should be recieving data from the DB, 2 pieces to be exact from the table known as "members"
except it isnt, can someone suggest a reason as to why? because i have looked through it and it still isnt working :banghead:
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: hmm, works but doesnt...

Post by Reviresco »

Line 44: $query_data is not an array -- it's a number. Which probably means that $numrows is empty and is throwing off everything.
Monotoko
Forum Commoner
Posts: 64
Joined: Fri Oct 26, 2007 4:24 pm

Re: hmm, works but doesnt...

Post by Monotoko »

so how do i get the number of rows...?
thePhpForum
Forum Newbie
Posts: 2
Joined: Thu Feb 28, 2008 11:56 am
Location: Canada

Re: hmm, works but doesnt...

Post by thePhpForum »

Hi, I think you are doing something wrong. If you would like to know number of all records in your table there are 2 basic ways how to do it:

$result = mysql_query("SELECT * FROM members");
$numrows = mysql_num_rows($result); // this returns number of rows in the result

In this first example I would recomend not to use '*' in the SELECT but use something like "SELECT id FROM members".

$result = mysql_query("SELECT COUNT(id) FROM members");
$query_data = mysql_fetch_array($result);
$numrows = $query_data[0];

I hope this helps.
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: hmm, works but doesnt...

Post by Reviresco »

Monotoko wrote:so how do i get the number of rows...?

Code: Select all

$numrows = mysql_num_rows($result);
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: hmm, works but doesnt...

Post by Zoxive »

Code: Select all

/* #55 */ $result = mysql_query($query, $con);
/* #56 */ $name = $row['username'];
You query it, but you never fetch the data.

It looks like you want to put..

Code: Select all

$row = mysql_fetch_assoc($result);
Below the query.
Post Reply