mysql LIMIT

C++, C#, Java, PHP, ect...
Post Reply
Zester
Posts: 53
Joined: Sat Mar 03, 2012 11:33 pm

mysql LIMIT

Post 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

User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: mysql LIMIT

Post 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.
The indelible lord of tl;dr
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: mysql LIMIT

Post 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"; 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: mysql LIMIT

Post 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
The indelible lord of tl;dr
Post Reply

Return to “Coding”