Page 1 of 2

next page

Posted: Thu Sep 16, 2004 4:42 am
by sguy
this is a searching function
the max result is 30, if more than 30, it has "- Prev 1 2 Next -"
it shows the correct result, but if i press next, it shows these errors:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\phpweb\searching.php on line 61

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in c:\phpweb\searching.php on line 90

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\phpweb\searching.php on line 133

Code: Select all

<?PHP
// $reqlevel sets the accesslevel a user needs to hace
// use 0 (zero) if you want to make it an admin only page.
// and 1 (one)  is default for an new user. 
// this makes the page an member only page.
include("config.php");
$search = $HTTP_GET_VARS["search"];
$searchname = $HTTP_POST_VARS["searchname"];
$name = $HTTP_POST_VARS["name"];
$gender = $HTTP_POST_VARS["gender"];
$state = $HTTP_POST_VARS["state"];
$lowage = $HTTP_POST_VARS["lowage"];
$highage = $HTTP_POST_VARS["highage"];
$image = $HTTP_POST_VARS["image"];
$page = $HTTP_POST_VARS["page"];

?>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<LINK href="help.css" rel=stylesheet type=text/css>

</head>

<body bgcolor="#FFFFFF" text="#000000">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>
      <?
$currentyear=$currentyear=strftime("%Y");
?>
    </td>
  </tr>
  <tr> 
    <td> 
      <?php

    mysql_select_db($database, $conn);
    if(!$conn)
    {
       echo "Couldn't make a connection!!!";
       exit;
    }

    $db = mysql_select_db($database,$conn);
    if(!$db)
    {
      echo "The database disapeared!";
      mysql_close($conn);
      exit;
    }

$image="";
if(isset($_POST['image']) && $_POST['image'] == 1) { 
$image .= ' AND image = 1 ';  
}
    $max = 0;
    $bmax = mysql_query("SELECT * from ".$DBprefix."signup WHERE username like '%$searchname%' and fullname like'%$name%' and state like'%$state%' and gender like'$gender%' and userlevel='1' and actnum='0' and '$currentyear'-dobyear >= $lowage and '$currentyear'-dobyear <= '$highage' {$image}");
    while ($row = mysql_fetch_array($bmax))
    {
      $max++;
    }
    ?>
	<font size="2">
      <?php
	  $image="";
if(isset($_POST['image']) && $_POST['image'] == 1) { 
$image .= ' AND image = 1 ';  
} 
echo"<table border=0 cellspacing=0 width=80% align=center>";
    echo "</td><td height="12" align="right" bgcolor="lightcyan" width="100%"><font size=2>";

    echo "<center>Database record returned<font color = blue> $max </font>
          items.</center>";
    echo "</td></tr></form></table><br>";


    $maxresult = 30;
    $sql_text = ("SELECT * from ".$DBprefix."signup WHERE username like '%$searchname%' and fullname like'%$name%' and state like'%$state%' and gender like'$gender%' and userlevel='1' and actnum='0' and '$currentyear'-dobyear >= $lowage and '$currentyear'-dobyear <= '$highage' {$image}");

    if (!$page) {
    $page = 1;
    } 
    $backpage = $page - 1;
    $nextpage = $page + 1;
    $query = mysql_query($sql_text);
    $start = ($maxresult * $page) - $maxresult; 
    $num_rows = mysql_num_rows($query); 
    if ($num_rows <= $maxresult) {
    $num_pages = 1; 
    } else if (($num_rows % $maxresult) == 0) {
      $num_pages = ($num_rows / $maxresult);
    } else {
    $num_pages = ($num_rows / $maxresult) + 1;
    } 
    $num_pages = (int) $num_pages;
    if (($page > $num_pages) || ($page < 0)) {
    error("You have specified an invalid page number");
    }
    $sql_text = $sql_text . " LIMIT $start, $maxresult";
    $query = mysql_query($sql_text);
    
    if ($max>$maxresult)
    {
    echo "<center>- ";
    if ($backpage) { 
    echo "<a href="searching.php?search=$search&page=$backpage">Prev</a>";
    } else {
    echo "Prev";
    }
    for ($i = 1; $i <= $num_pages; $i++) {
    if ($i != $page) { 
    echo " <a href="searching.php?search=$search&page=$i">$i</a> ";
    } else { 
    echo " $i "; 
    } 
    }
    if ($page != $num_pages) {
    echo "<a href="searching.php?search=$search&page=$nextpage">Next</a> -";
    } else {
    echo "Next -";
    }
    echo "</center>";
    }
    ?><CENTER>
<table CELLSPACING="2" CELLPADDING="2" BORDER="0" width="70%"> 
<font size=2>
<?php 
$a = $start + 1; 
     print "  <TR bgcolor=slateblue><TD font size=2 align=center height="26"><font font size=2 color=white><b>Nickname</TD><TD align=center><font size=2 color=white><b>Gender</TD><TD align=center><font size=2 color=white><b>Age</TD><TD align=center><font size=2 color=white><b>Location</TD><TD align=center><font size=2 color=white><b>Country</TD><TD align=center><font size=2 color=white><b>Headline</TD></TR>\n"; 
while ($row = mysql_fetch_array($query)) {
?>
<?php
$image="";
if(isset($_POST['image']) && $_POST['image'] == 1) { 
$image .= ' AND image = 1 ';  
}
$res = mysql_query("SELECT * FROM ".$DBprefix."signup WHERE username like '%$searchname%' and fullname like'%$name%' and state like'%$state%' and gender like'$gender%' and userlevel='1' and actnum='0' and '$currentyear'-dobyear >= $lowage and '$currentyear'-dobyear <= '$highage' {$image}");  
if(mysql_num_rows($res) > 0) {  
      echo"<tr>"; 
	  $pic = strtolower(substr($row['gender'], 0, 1)); 
   		$color = ($pic == 'm') ? 'aliceblue' : '#fff0ff';
      echo '<td bgcolor="'.$color.'"><font size=2>&nbsp;&nbsp;<b><a class=hq href="viewuser.php?name='.$row['username'].'" target="_blank">'.$row['username'].'</a>';  
         if($row['image'] == 1) {   
         echo '&nbsp;&nbsp;<img src="members/image/camera.gif" alt="camera" />';  
         }  
          echo '</td>'."\n";  
		  $pic = strtolower(substr($row['gender'], 0, 1)); 
   		$color = ($pic == 'm') ? 'aliceblue' : '#fff0ff';
    		"$row[gender]\n";
	  $currentyear=strftime("%Y");
	  $currentdate=strftime("%d-%m-%Y");
	  $age = $currentyear - $row[dobyear];
		$pic = strtolower(substr($row['gender'], 0, 1)); 
   		$color = ($pic == 'm') ? 'aliceblue' : '#fff0ff';
		echo'<td align=center bgcolor="'.$color.'">';
    		"$row[gender]\n";
		if($row['gender'] == 'male') {
		echo '<img src="members/image/'.substr($row['gender'], 0, 1).'.jpg" alt="'.$row['gender'].'" />';
		}
		else {
		echo '<img src="members/image/'.substr($row['gender'], 0, 1).'.jpg" alt="'.$row['gender'].'" />';
		}
$pic = strtolower(substr($row['gender'], 0, 1)); 
   		$color = ($pic == 'm') ? 'aliceblue' : '#fff0ff';
		echo'<td align=center bgcolor="'.$color.'">';
		echo"<font size=2>$age";
    		"$row[gender]\n";
$pic = strtolower(substr($row['gender'], 0, 1)); 
   		$color = ($pic == 'm') ? 'aliceblue' : '#fff0ff';
		echo'<td align=center bgcolor="'.$color.'">';
		echo"<font size=2>$row[location]";
    		"$row[gender]\n";
$pic = strtolower(substr($row['gender'], 0, 1)); 
   		$color = ($pic == 'm') ? 'aliceblue' : '#fff0ff';
		echo'<td align=center bgcolor="'.$color.'">';
		echo"<font size=2>$row[country]";
    		"$row[gender]\n";			
$pic1 = strtolower(substr($row['gender'], 0, 1)); 
   		$color1 = ($pic1 == 'm') ? 'blue' : 'red';
		echo'<font color="'.$color1.'">';
$pic = strtolower(substr($row['gender'], 0, 1)); 
   		$color = ($pic == 'm') ? 'aliceblue' : '#fff0ff';
		$color1 = ($pic1 == 'm') ? 'blue' : 'red';
		echo'<td bgcolor="'.$color.'"><font color="'.$color1.'">';
		echo"<font size=2>$row[headline]";
    		"$row[gender]\n";	
$a++; 
   }  
else {  
   echo 'No users found';  
} 
?>
</body>
</html>
<?php
    }
    echo "</table>";
    if ($max>$maxresult)
    {
    echo "<br><center>- ";
    if ($backpage) { 
    echo "<a href="searching.php?search=$search&page=$backpage">Prev</a>";
    } else {
    echo "Prev";
    }
    for ($i = 1; $i <= $num_pages; $i++) {
    if ($i != $page) { 
    echo " <a href="searching.php?search=$search&page=$i">$i</a> ";
    } else { 
    echo " $i "; 
    } 
    }
    if ($page != $num_pages) {
    echo "<a href="searching.php?search=$search&page=$nextpage">Next</a> -";
    } else {
    echo "Next -";
    }
    echo "</center>";
    }
?></font>

Posted: Thu Sep 16, 2004 5:03 am
by CoderGoblin
At a guess your SQL query is failing. Check what the SQL query is, where necessary output the SQL statement and check it is as expected.

I would also recommend trying to track the problem down a bit and only posting the relevent sections. I for one will not try to go through all that code.

Posted: Thu Sep 16, 2004 5:53 am
by dethron
$lowage and $highage variables are not posted, when you click next.
And if you add this variables to next-link, then you must change the way that you are taking variables.

Code: Select all

<?php
$lowage = $HTTP_POST_VARS["lowage"];
$highage = $HTTP_POST_VARS["highage"];
?>

Posted: Thu Sep 16, 2004 6:09 am
by John Cartwright
or die(mysql_error());

Posted: Thu Sep 16, 2004 6:14 am
by dethron
Annnndddd,
if you want us to help you, then help us :)

do some observations on viewtopic.php?p=133634, there are line76, and a better version of it also.

as you can see, if you write it in 12-lines instead of messing everything to a one-line; it is easier to track the code and find mistakes.

Also you may change the 3th line which is

Code: Select all

$sqlstatement = "SELECT * FROM $tablename WHERE
as

Code: Select all

echo $sqlstatement = "SELECT * FROM $tablename WHERE
to see the sql statement :)


Let me say one more thing, when you see a warning like that "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\phpweb\searching.php on line 61 ", it means your query is wrong. Then write the query to screen (using echo for example) and check it also by sending directly to sql-server if you can't catch the problmes by your eyes :)

Good Luck.

Posted: Thu Sep 16, 2004 11:05 am
by feyd
it may not be the query that's wrong.. it's possible he's not connected:

Code: Select all

mysql_select_db($database, $conn);
    if(!$conn)
    {
       echo "Couldn't make a connection!!!";
       exit;
    }

    $db = mysql_select_db($database,$conn);
    if(!$db)
    {
      echo "The database disapeared!";
      mysql_close($conn);
      exit;
    }

Posted: Fri Sep 17, 2004 12:52 am
by sguy
although i put these code, oso can't work..

$lowage = $HTTP_POST_VARS["lowage"];
$highage = $HTTP_POST_VARS["highage"];

Posted: Fri Sep 17, 2004 3:55 am
by dethron
waht is the query? can you echo it to the screen then tell us, so we can decide whether query is wrong or not?

Posted: Fri Sep 17, 2004 5:11 am
by sguy
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\phpweb\searching.php on line 64

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in c:\phpweb\searching.php on line 94

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\phpweb\searching.php on line 137

Posted: Fri Sep 17, 2004 5:12 am
by feyd
that's not what dethron asked for. :?

Posted: Fri Sep 17, 2004 7:35 am
by sguy
if i put every echo in front of query.....

Code: Select all

echo $bmax = mysql_query("SELECT * from ".$DBprefix."signup WHERE username like '%$searchname%' and fullname like'%$name%' and state like'%$state%' and gender like'$gender%' and userlevel='1' and actnum='0' and '$currentyear'-dobyear >= $lowage and '$currentyear'-dobyear <= '$highage' {$image}");
Resource id #3

---------

Code: Select all

echo $sql_text = ("SELECT * from ".$DBprefix."signup WHERE username like '%$searchname%' and fullname like'%$name%' and state like'%$state%' and gender like'$gender%' and userlevel='1' and actnum='0' and '$currentyear'-dobyear >= $lowage and '$currentyear'-dobyear <= '$highage' {$image}");
SELECT * from ALS_signup WHERE username like '%%' and fullname like'%%' and state like'%%' and gender like'%' and userlevel='1' and actnum='0' and '2004'-dobyear >= 18 and '2004'-dobyear <= '99'

----------

Code: Select all

echo $res = mysql_query("SELECT * FROM ".$DBprefix."signup WHERE username like '%$searchname%' and fullname like'%$name%' and state like'%$state%' and gender like'$gender%' and userlevel='1' and actnum='0' and '$currentyear'-dobyear >= $lowage and '$currentyear'-dobyear <= '$highage' {$image}");
Resource id #6

Posted: Fri Sep 17, 2004 7:49 am
by patrikG
Have a look at this thread: viewtopic.php?t=23227&start=18
and read up in the manual under mysql_query.

Posted: Sun Sep 19, 2004 1:53 am
by sguy
when i put "or die('error: '. mysql_error());", it shows this error message:

error: You have an error in your SQL syntax near 'and '2004'-dobyear <= '' ' at line 1

Posted: Sun Sep 19, 2004 2:09 am
by feyd
looks like $highage isn't set, or is set wrong...

Re: next page

Posted: Sun Sep 19, 2004 7:49 pm
by dethron
sguy wrote:

Code: Select all

<?php
    echo "<a href="searching.php?search=$search&page=$nextpage">Next</a> -";
?>
When next clicked, the value of $highage is not send.
Also it is same for Prev-link.