Mahngiel Posted March 10, 2011 Share Posted March 10, 2011 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 https://forums.phpfreaks.com/topic/230255-not-establishing-correct-mysql-limit/ Share on other sites More sharing options...
AbraCadaver Posted March 10, 2011 Share Posted March 10, 2011 Try: $max = $count['COUNT(*)']; $min = $max - 5; $said = mysql_query("SELECT * FROM shoutbox ORDER BY date ASC LIMIT $min, $max") ; Or, removing the quotes around $count might work. Link to comment https://forums.phpfreaks.com/topic/230255-not-establishing-correct-mysql-limit/#findComment-1185749 Share on other sites More sharing options...
Mahngiel Posted March 10, 2011 Author Share Posted March 10, 2011 not working any way i use variables after LIMIT. using integers works fine but not vars. there a better way to do this? Link to comment https://forums.phpfreaks.com/topic/230255-not-establishing-correct-mysql-limit/#findComment-1185753 Share on other sites More sharing options...
PFMaBiSmAd Posted March 10, 2011 Share Posted March 10, 2011 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 https://forums.phpfreaks.com/topic/230255-not-establishing-correct-mysql-limit/#findComment-1185761 Share on other sites More sharing options...
Mahngiel Posted March 10, 2011 Author Share Posted March 10, 2011 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 https://forums.phpfreaks.com/topic/230255-not-establishing-correct-mysql-limit/#findComment-1185768 Share on other sites More sharing options...
AbraCadaver Posted March 10, 2011 Share Posted March 10, 2011 Don't use quotes, and what does this yield? echo "SELECT * FROM shoutbox ORDER BY date ASC LIMIT $min, $max"; Link to comment https://forums.phpfreaks.com/topic/230255-not-establishing-correct-mysql-limit/#findComment-1185774 Share on other sites More sharing options...
Mahngiel Posted March 10, 2011 Author Share Posted March 10, 2011 Thank you. I suppose you only need to quote it off in the query when it's being compared? Link to comment https://forums.phpfreaks.com/topic/230255-not-establishing-correct-mysql-limit/#findComment-1185780 Share on other sites More sharing options...
AbraCadaver Posted March 10, 2011 Share Posted March 10, 2011 Thank you. I suppose you only need to quote it off in the query when it's being compared? When it's a string. In this case it's an integer and MySQL expects an integer. Link to comment https://forums.phpfreaks.com/topic/230255-not-establishing-correct-mysql-limit/#findComment-1185781 Share on other sites More sharing options...
Mahngiel Posted March 10, 2011 Author Share Posted March 10, 2011 Thanks! _ ( (( \ =\ __\_ `-\ (____))( \---- (____)) _ (____)) (____))____/---- Link to comment https://forums.phpfreaks.com/topic/230255-not-establishing-correct-mysql-limit/#findComment-1185783 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.