-6

I am trying to fetch data as json between two id's which have userid either Get['id'] or user who follow Get['id'] from other Table.

My URL is - http://localhost/aditya/m/login?id=1&&start=6

if(isset($_GET['start'])) {
$query2 = "SELECT * FROM post WHERE (id BETWEEN '".$_GET['start']."' AND '".$_GET['start']."' + 3) AND userid = '".$_GET['id']."' OR userid IN (SELECT userid FROM frndlist WHERE followers = '".$_GET['id']."') "; }

Data are coming in between id 6 to 9 but why 5 is also coming ?? (postid in json as id) !!

{
   "result":[
      {
         "like":"</a>",
         "like_no":"0</span>",
         "time":"th , ",
         "pro_pic":"members/boykiller654@gmail.com/WIN_20150115_125206.JPG",
         "userid":"2",
         "fname":"Aditya",
         "lname":"Raj",
         "share":"innovation",
         "title":"tasty",
         "postid":"5",
         "postimg":"http://192.168.43.142/aditya/mfoodora/members/boykiller654@gmail.com/download-371493367.jpg",
         "desc":"hejgfiyrivbgdyvdgvgjdehuidkjvdakllj",
         "comm_no":"0",
         "comment":"</a>",
         "add":"</a>"
      },
      {
         "like":"</a>",
         "like_no":"0</span>",
         "time":"th , ",
         "pro_pic":"members/boykiller654@gmail.com/WIN_20150115_125206.JPG",
         "userid":"2",
         "fname":"Aditya",
         "lname":"Raj",
         "share":"trick",
         "title":"tasty",
         "postid":"6",
         "postimg":"",
         "desc":"hejgfiyrivbgdyvdgvgjdehuidkjvdakllj",
         "comm_no":"0",
         "comment":"</a>",
         "add":"</a>"
      },
      {
         "like":"</a>",
         "like_no":"1</span>",
         "time":"Mar 24th",
         "pro_pic":"members/boykiller654@gmail.com/WIN_20150115_125206.JPG",
         "userid":"2",
         "fname":"Aditya",
         "lname":"Raj",
         "share":"recipe",
         "title":"tasty",
         "postid":"7",
         "postimg":"",
         "desc":"hejgfiyrivbgdyvdgvgjdehuidkjvdakllj",
         "comm_no":"0",
         "comment":"</a>",
         "add":"</a>"
      },
      {
         "like":"</a>",
         "like_no":"0</span>",
         "time":"Mar 25th",
         "pro_pic":"members/aditraj2@gmail.com/WIN_20150115_140702.JPG",
         "userid":"1",
         "fname":"Joe",
         "lname":"Harris",
         "share":"Trick",
         "title":"Chicken Hyderabadi Biryani",
         "postid":"9",
         "postimg":"http://192.168.43.142/aditya/mfoodora/members/aditraj2@gmail.com/chocolate-mint-bar-1231354420.jpg",
         "desc":"So , here it goes .. It's a very hybrid biryani especially found in India . Vey famous and tasty but...Read More</a>",
         "comm_no":"1",
         "comment":"</a>",
         "add":"</a>"
      }
   ]
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Aditya Raj
  • 168
  • 3
  • 9
  • Can you please share your schema and some sample response – Kailash Yadav Apr 29 '15 at 16:01
  • ok, you told us what you're trying to do. Now what's the problem? – Rick S Apr 29 '15 at 16:02
  • Getting Wrong answer , BETWEEN is not working.. – Aditya Raj Apr 29 '15 at 16:05
  • Try `echo` on this and then paste that additionally into the question. As it stands we can't read the query easily. Also, please try that in your database directly (e.g. MySQL on the console, or phpMyAdmin). It also has a SQL injection vuln, which you must fix before going live. – halfer Apr 29 '15 at 16:06
  • (I would recommend writing your SQL strings over many lines, incidentally, so as to make them much more readable both in your code and when you paste them here). – halfer Apr 29 '15 at 16:08
  • @Tom: the quote device `>` is not really suitable for JSON strings. I suggest running badly formatted JSON [through a formatter](http://jsonformatter.curiousconcept.com/) and then pasting as code (of course, the OP should do this, but still). – halfer Apr 29 '15 at 16:32
  • @AdityaRaj: your question is not very clear, and has needed quite a bit of repair to make it more readable. I have reluctantly downvoted. We still need to see the SQL without the PHP, as I have already advised. – halfer Apr 29 '15 at 16:33

2 Answers2

1

Try grouping your OR statement together by putting it in parenthesis.

SELECT * 
FROM post 
WHERE (id BETWEEN '1' AND '500' + 5) AND 
(userid = 'testuserid' OR 
userid IN (SELECT user FROM frndlist WHERE followers = 'tom') )
Rick S
  • 6,476
  • 5
  • 29
  • 43
0

AND has higher precedence than OR.

WHERE cond1 AND cond2 OR cond3

is interpreted as

WHERE ( cond1 AND cond2 ) OR cond3

Just like 3 * 5 + 4 is interpreted as (3 * 5) + 4

So, as long as the last condition is true, the row will be included.

Ada Lovelace
  • 835
  • 2
  • 8
  • 20