Forum Moderators: coopster

Message Too Old, No Replies

Sql statment skipping first statment

Sql statment skipping first statment

         

BlackRaven

6:40 pm on Jul 29, 2004 (gmt 0)

10+ Year Member



yeah hi, ok for some reason
$row = mysql_fetch_row($result)
{print $row[0];}
is fialing the print the first value of the search result is prints the others fine.
$query = "SELECT * FROM $table " . $where_query ." ORDER BY ". $orderby." LIMIT $l1, $l2";

BlackRaven

6:41 pm on Jul 29, 2004 (gmt 0)

10+ Year Member



sorry about all the grammer mistakes just didn't drink coffee yet.

ergophobe

7:48 pm on Jul 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In your limit clause, are you counting row 1 as 0?

LIMIT 0, 10 (rows 1-10)
LIMIT 1, 10 (rows 2-11)

Tom

BlackRaven

8:53 pm on Jul 29, 2004 (gmt 0)

10+ Year Member



not really sure i am using the below code for paging.

$page = (int) $_GET["page"];
if (($page<0) ¦¦ ($page=="")) $page=0;
$epp = 10; /* entries per page */

$l1 = $epp*$page;
$l2 = $epp;

$query = "SELECT * FROM $table " . $where_query ." ORDER BY ". $orderby." LIMIT $l1, $l2";

ergophobe

9:23 pm on Jul 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So on page 1,you should be getting rows 11-20.

I suspect you want page 1 to give you rows 1-10 right? So you need

$page = $_GET["page"] - 1;

BlackRaven

11:59 pm on Jul 29, 2004 (gmt 0)

10+ Year Member



ok double checked i now doubt it has to do something with the paged addon

list($totalrows) = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM $table " . $where_query));

in the above code $totalrows shows as 6 while

$query = "SELECT * FROM $table " . $where_query ." ORDER BY ". $orderby

only shows 5 entries (i removed the lImit) making me belive that i am doing something wrong in displaying the table enteries it self.

i suspect it has to something with my while statment when i am printing the database entries

========================
$page = $_GET["page"];
if (($page<0) ¦¦ ($page=="")) $page=0;
$epp = 2; /* entries per page */

$l1 = $epp*($page);
$l2 = $epp;

list($totalrows) = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM $table " . $where_query));

$query = "SELECT * FROM $table " . $where_query ." ORDER BY ". $orderby." LIMIT $l1, $l2";

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

if(mysql_fetch_row($result)=="0")
{
print("<tr><td colspan='7' align='center'>Sorry no results found. Please try your search again.</td></tr>");
}
else{
print("<tr><td colspan='7' align='center'>");
print($totalrows." Results Found.<br/>");

if($page!=0){
print("<a href='$PHP_SELF?page=".($page-1)."&search=".$search."&searchfield="
.$searchfield."&bookfor=".$bookfor."&orderby=".$orderby."&country=".$country."
&school=".$school."&iprefer=".$iprefer."' alt='Previous Page' title='Previous Page'>[ Previous ]</a>");
}

$totalpages =(int)( $totalrows / $epp);

print(" Page # ".($page+1)." ");

if(($page+1)!=$totalpages && ($totalpages!=0)){

print("<a href='$PHP_SELF?page=".($page+1)."&search=".$search."&searchfield="
.$searchfield."&bookfor=".$bookfor."&orderby=".$orderby."&country=".$country."
&school=".$school."&iprefer=".$iprefer."' alt='Next Page' title='Next Page'>[ Next ]</a>");
}

while($row = mysql_fetch_row($result)){
trimmed irrelevant code to fix sidescroll
}

mysql_close();
}

[edited by: coopster at 1:38 pm (utc) on July 30, 2004]

coopster

1:52 pm on Jul 30, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The problem lies in that you are reading a row from your result set earlier up in your code, and not doing anything with it. Therefore the next read from the result set is going to be the second row.
$result = mysql_query($query) or die("SQL Error: " . mysql_error() ); 
if(mysql_fetch_row($result)=="0") {

At this point, you have fetched a row from your result set. Then you don't do anything with it. Now, the internal pointer is staged to read the next row in the result set, so when you fire up your
while
loop, it is going to start with row #2 of the result set.
while ($row = mysql_fetch_row($result)) { 
Make sense?

You have a couple of options. You could either use mysql_data_seek() [php.net] to move the internal row pointer of the MySQL result associated with the specified result identifier to point to the specified row number. Or, better yet, use mysql_num_rows [php.net] to get the number of rows in the result set to test in your

if
statement as this will not advance the internal pointer of the result set on you.
$result = mysql_query($query) or die("SQL Error: " . mysql_error() ); 
if(mysql_num_rows($result)=="0") {

BlackRaven

7:21 pm on Jul 30, 2004 (gmt 0)

10+ Year Member



thanks coopster that did the trick :)