Jump to content

Not establishing correct mysql Limit


Mahngiel

Recommended Posts

I feel like a tard for having to ask this, but I've spent way too much time trying to figure this out, so i've brought it to you guys.

 

I have run into a an error running a while loop off a query:

$rowcount= mysql_query("SELECT COUNT(*) FROM `shoutbox`");
$count = mysql_fetch_array($rowcount);
$count = $count['COUNT(*)'];
$said = mysql_query("SELECT * FROM shoutbox ORDER BY date ASC LIMIT ('$count' - 5),'$count' ");

while($row = mysql_fetch_array( $said )){  //line 14 in the code
echo $row['shouter'].':  '.$row['contents'].'<br />'; 
}

mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mahngiel/public_html/shout.php on line 14

This script works when I remove the limit counts, but then I have to descend the order and things are not in the position I want them in.  I'm sure the problem is the limit vars, but dunno which way to go to fix it. thanks

Link to comment
Share on other sites

You need to test and limit the value to a positive number ( a negative number in the LIMIT is an error).

 

It would also help if you formed the query in a php variable so that you could echo the actual query as part of the error checking logic on your query, oops, assuming that you had some error checking logic in your code.

Link to comment
Share on other sites

I have echo'd everything during this process to ensure validity and error processing.  The problem is occurs when setting the LIMIT to variables.  here is my full testing code:

<?php
include('header.php');

$shout = "INSERT INTO shoutbox (shouter, contents) VALUES ('".$_POST['sayer']."','".$_POST['say']."')";
if (isset($_POST['Submit'])) { mysql_query($shout);}
$rowcount= mysql_query("SELECT COUNT(*) FROM `shoutbox`");
$count = mysql_fetch_array($rowcount);
$max = $count['COUNT(*)'];
$min = $max - 5;
$said = mysql_query("SELECT * FROM shoutbox ORDER BY date ASC LIMIT '$min','$max' ");

while($row = mysql_fetch_array($said)){  //line 12
echo $row['shouter'].':  '.$row['contents'].'<br />'; 
}
?>
</div></div>
<form action="shout.php" method="POST" name="shoutbox">
<input type="hidden" name="sayer" value="<?php echo $name;?>" />
<input type="text" name="say" width="180px" />
<input type="submit" name="submit" value="Say" />
</form>

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mahngiel/public_html/shout.php on line 12

 

this works just fine by changing $min, $max to 0, 5.

 

Adding these lines:

echo 'min '.$min.'<br/>';
echo 'max '.$max;

gives me

min 3
max 8
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mahngiel/public_html/shout.php on line 13

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.