Jump to content

Problem using LIMIT in SELECT statement


rocky48
Go to solution Solved by Psycho,

Recommended Posts

I have got a set of data for a country for each week of the year, but want to display the data a quarter at a time, using a form which posts a value into LIMIT, which was OK for the 1st quarter, but for subsequent quarters I needed to use LIMIT in the form LIMIT start, rows.

I found by experiment that this had to be the last statement in the query.

when I added the start I got an error:

Parse error: syntax error, unexpected ',' in /homepages/43/d344817611/htdocs/Admin/Visitdata.php on line 10

Here is the SQL part of my code:

$Visit_data="SELECT WeekNo.WNo, WeekNo.WCom, Countries.Country, ctryvisits.CVisits FROM ctryvisits 
LEFT JOIN Countries
ON ctryvisits.country=Countries.CID
LEFT JOIN WeekNo
      ON ctryvisits.WNo=WeekNo.WNo
      WHERE Countries.Country = '".$_POST["Country"]."'
ORDER BY ctryvisits.WNo LIMIT '".$_POST["QUARTER"]."'" - 13, '".$_POST["QUARTER"]."'";

Is this the best way of doing what I require or is there an easier way of achieving what I want?

Link to comment
Share on other sites

  • Solution

Also, when defining strings with variables, I prefer to use the double-quotes (as you have done) and include the variables {inside} the quoted string. Always going in and out of the quotes and concatenating content makes it harder to see these errors.

 

Also, how you are defining the limit value doesn't look correct. It looks like you are putting the numerical values in quotes and also trying to do a subtraction. These should not be in quotes. You could probably do the subtraction, but would probably want to put it in parenthesis. But, I would do the match before creating the query.

 

 

$limitStart = $_POST['QUARTER'] - 13;
$query="SELECT WeekNo.WNo, WeekNo.WCom, Countries.Country, ctryvisits.CVisits
        FROM ctryvisits 
        LEFT JOIN Countries
            ON ctryvisits.country = Countries.CID
        LEFT JOIN WeekNo
            ON ctryvisits.WNo=WeekNo.WNo
        WHERE Countries.Country = '{$_POST['Country']}'
        ORDER BY ctryvisits.WNo
        LIMIT {$limitStart}, {$_POST['QUARTER']}";

 

By the way, this is wide open to SQL Injection. Definitely look into prepared statements.

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.