Page 1 of 1

mysql LIMIT

Posted: Wed Mar 21, 2012 1:27 am
by Zester
HI All,

I just can not get the LIMIT to work, I comment it out then the code work find.

Code: Select all

if ($_SESSION['playeraccess'] >= 2) {
	  	$charslots = "$invenlordmax";
  	}
else {
		$charslots = "$invenmax";
	}
		$query = "select * from `lqlotl.inventory` where `inventory.charid` = `$charid` limit 0,`$charslots` ";
  $result = mysql_query($query) or die("Could not find chars invetory ");
  $num_rows = mysql_num_rows($result);
  $result2 = mysql_fetch_array($result);
  if ($result2) {
		echo " <center>
	<h1>" . $charname . "s Inventory slots </h1><br /><br />
	<h2> Used: " . $num_rows . " out off " .  $charslots . " <h2>
	<table border='2' >
	  	<tr>
    	<th scope='col' >Slot<font color='#FFFFFF'>____ </font></th>
    	<th scope='col'>Name<font color='#FFFFFF'>____ </font


Re: mysql LIMIT

Posted: Wed Mar 21, 2012 3:18 am
by Jackolantern
Right after this line:

Code: Select all

$query = "select * from `lqlotl.inventory` where `inventory.charid` = `$charid` limit 0,`$charslots` "; 
echo out $query. Inspect the final SQL query produced. You can even try copy and pasting it and typing it into the SQL tab in phpMyAdmin to see how the direct SQL query produced by your code performs. If it works as expected there, then the problem is elsewhere in the script.

This is generally how I go about debugging MySQL issues in PHP 8-)

EDIT: Oh, and of course you should be using mysql_error() and mysql_errno() to get specific mysql errors right in your PHP script.

Re: mysql LIMIT

Posted: Wed Mar 21, 2012 9:07 am
by Chris
I think the problem lies in the `'s: Only use them when your are getting fields, tables or database names. Not when passing numbers or other data.

Code: Select all

$query = "select * from `lqlotl.inventory` where `inventory.charid` = `$charid` limit 0,`$charslots`"; 
Should be

Code: Select all

$query = "select * from `lqlotl.inventory` where `inventory.charid` = `$charid` limit 0,$charslots"; 

Re: mysql LIMIT

Posted: Wed Mar 21, 2012 5:38 pm
by Jackolantern
Ohhh yeah, I forgot that was going to be a number. Yes, that will cause a problem. Numbers in SQL queries must be "raw", with no quotation marks or back-ticks or they are taken as strings, which will cause a type error in MySQL