Jump to content

[SOLVED] num_rows error help


Clinton

Recommended Posts

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource on line 98 and 108

 

<?php
			if (!(isset($pagenum)))
			{ $pagenum = 1; }
    			$sql = "SELECT * FROM thelist WHERE dmajorp = '$smajor' ORDER BY jposted";
			$rs = mysql_query($sql);
			$rows = mysql_num_rows($sql); ///HERE///
			$page_rows = 4;
			$last = ceil($rows/$page_rows);
			if ($pagenum < 1)
			{ $pagenum = 1; }
			elseif ($pagenum > $last)
			{ $pagenum = $last;}
                $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
                $sql_p = "SELECT * FROM thelist WHERE dmajorp = '$smajor' ORDER BY jposted '$max'";
			if(mysql_num_rows($rs) > 0){
			while($row = mysql_fetch_array($sql_p)) ///AND HERE///
			{
			extract($row);
			?>

 

I'm trying to paginate and then echo an else if there are no rows (hence the two statements. Not sure how to fix this.

Link to comment
Share on other sites

Extremely common problem, try searching the forum for:

 

"Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource" ..

 

Try switching:

 

$rs = mysql_query($sql);

 

To:

 

$rs = mysql_query($sql) or die(mysql_error());

 

A

Link to comment
Share on other sites

Hah beat me to the first mistake there. And for the second you have't executed the query. Should be:

 

$sql_p = mysql_query("SELECT * FROM thelist WHERE dmajorp = '$smajor' ORDER BY jposted '$max'");

 

A

Link to comment
Share on other sites

Yea, I just got that too. :-) Now I'm not getting those errors anymore but it's not working correctly and it has something to do with that $max variable.

 

"The first thing we do is re-run our query from earlier, only with one slight change. This time we are including the $max variable to limit the query results to those that belong on our current page. After your query you would display your results as normal (using any formatting you wish.)"

 

Their Example - $data_p = mysql_query("SELECT * FROM topsites $max") or die(mysql_error());

 

:-| Any idea about this?

Link to comment
Share on other sites

Ok, gotcha. Now it is limiting the search results correctly but it is not displaying the code on the bottom that allows me to go to the next page. Any ideas there?

 

		<table width="95%" border="0" cellspacing="0" cellpadding="1" class="boxtitle">
			<?php
			if (!(isset($pagenum)))
				{ $pagenum = 1; }
    			$sql = "SELECT * FROM thelist WHERE dmajorp = '$smajor' ORDER BY jposted";
			$rs = mysql_query($sql);
			$rows = mysql_num_rows($rs);
			$page_rows = 2;
			$last = ceil($rows/$page_rows);
			if ($pagenum < 1)
				{ $pagenum = 1; }
			elseif ($pagenum > $last)
			{ $pagenum = $last;}
                $max = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
                $sql_p = mysql_query("SELECT * FROM thelist WHERE dmajorp = '$smajor' ORDER BY jobposted $max");

			if(mysql_num_rows($rs) > 0){
			while($row = mysql_fetch_array($sql_p) or die(mysql_error()))
			{				extract($row);
			?>
			<thead>
			<tr><td>
			<a href="show.php?id=<?php echo $id ?>"><?php echo $jtitle ?></a>
		 	</td></tr>
			</thead>
			<tr><td> Blah Blah
			</td></tr>
<?php } ///EVERYTHING BELOW THIS IS NOT SHOWING///?>
			<? echo " --Page $pagenum of $last-- <p>";
			if ($pagenum == 1)
			{}	else 	{
			echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
			echo " ";
			$previous = $pagenum-1;
			echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
						}
			echo " ---- ";

			if ($pagenum == $last)
			{} else 	{
			$next = $pagenum+1;
			echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
			echo " ";
			echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
			}
?>

		</table>
<?php } ?>

Link to comment
Share on other sites

<?php } ///EVERYTHING BELOW THIS IS NOT SHOWING///
            echo " --Page $pagenum of $last-- <p>";
            if ($pagenum == 1)
            {}   else    {
            echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
            echo " ";
            $previous = $pagenum-1;
            echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
                     }
            echo " ---- ";

            if ($pagenum == $last)
            {} else    {
            $next = $pagenum+1;
            echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
            echo " ";
            echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
            }
?>

         </table>
<?php } ?>

 

Try that.

Link to comment
Share on other sites

No, that didn't work either. But I just noticed that my footer isn't showing either so there's got to be something in there that's stopping the code period... ???

 

<div id="content">
		<div class="content-tl">
			<div class="content-tr">
				<div class="width">
					<div id="boxes">
						<div id="box1">
							<div class="module">
								<div class="first">
									<div class="sec min-height">
										<div class="box-indent">
											<div class="width">
											<h3><span><?php echo $smajor ?> So:</span></h3>

		<table width="95%" border="0" cellspacing="0" cellpadding="1" class="boxtitle">
			<?php
			if (!(isset($pagenum)))
				{ $pagenum = 1; }
    			$sql = "SELECT * FROM thelist WHERE dmajorp = '$smajor' ORDER BY jposted";
			$rs = mysql_query($sql);
			$rows = mysql_num_rows($rs);
			$page_rows = 2;
			$last = ceil($rows/$page_rows);
			if ($pagenum < 1)
				{ $pagenum = 1; }
			elseif ($pagenum > $last)
			{ $pagenum = $last;}
                $max = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
                $sql_p = mysql_query("SELECT * FROM thelist WHERE dmajorp = '$smajor' ORDER BY jposted $max");

			if(mysql_num_rows($rs) > 0){
			while($row = mysql_fetch_array($sql_p) or die(mysql_error()))
			{				extract($row);
			?>
			<thead>
			<tr><td>
			<a href="showad.php?id=<?php echo $id ?>"><?php echo $title ?></a> - <?php echo $sse ?> - Required: <?php echo $wex ?>
		 	</td></tr>
			</thead>
			<tr><td> <?php echo $poverview ?><br />
			</td></tr>
<?php } ?>
		<?php } ///EVERYTHING BELOW THIS IS NOT SHOWING///
            echo " --Page $pagenum of $last-- <p>";
            if ($pagenum == 1)
            {}   else    {
            echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
            echo " ";
            $previous = $pagenum-1;
            echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
                     }
            echo " ---- ";

            if ($pagenum == $last)
            {} else    {
            $next = $pagenum+1;
            echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
            echo " ";
            echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
            }
?>

		</table>

					</div>
				</div>
			</div>
		</div>
	</div>
</div>


			</div>

</div>
			</div>
		</div>
	</div>
</div>
</div>

	</div>
</div>

<div id="footer">
<div class="bg">
	<div class="right-bg">
		<div class="left-bg">
			<div class="space">
					© 2009

			</div>
		</div>
	</div>
</div>
</div>
</body>
</html>

Link to comment
Share on other sites

Whatever the problem is it is here:

 

<?php
            if (!(isset($pagenum)))
               { $pagenum = 1; }
             $sql = "SELECT * FROM thelist WHERE dmajorp = '$smajor' ORDER BY jposted";
            $rs = mysql_query($sql);
            $rows = mysql_num_rows($rs);
            $page_rows = 2;
            $last = ceil($rows/$page_rows);
            if ($pagenum < 1)
               { $pagenum = 1; }
            elseif ($pagenum > $last)
            { $pagenum = $last;}
                $max = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
                $sql_p = mysql_query("SELECT * FROM thelist WHERE dmajorp = '$smajor' ORDER BY jposted $max");

            if(mysql_num_rows($rs) > 0){
            while($row = mysql_fetch_array($sql_p) or die(mysql_error()))
            {            extract($row);
            ?>

 

Because when I take it out everything else (i.e. the footer) shows up just fine. But i'm not getting any errors. w...t...h...???

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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