2

I have variable $page in my PHP page and in a query I want to use this like:

$query = mysqli_query($con, "SELECT * FROM third LIMIT (3*($page-1)),3");
//right here I want to this math but it gives me error.

I tried it in that way too

$query = mysqli_query($con, "SELECT * FROM third LIMIT (3*{$page-1}),3");

It gave me that message:

syntax error, unexpected '-', expecting '}'

How should do this math in SQL?

2 Answers 2

5
$query = mysqli_query($con, "SELECT * FROM third LIMIT " . (3*($page-1)) . ",3");

Just calculate the value in PHP and concatenate :)

Sign up to request clarification or add additional context in comments.

3 Comments

Concat operator is . :)
@scrowler Right you are, working with java all day :)
Can you explain what doesn't work? Do you have an error or something alike?
1

Please note that your problem is not one of SQL, but of how PHP interpolates strings.

Your problem is that PHP will interpolate variables into strings, but not expressions.

You can do this as suggested above:

$query = mysqli_query($con, "SELECT * FROM third LIMIT " . (3*($page-1)) . ",3");

Or you could do this:

$limit = 3*($page-1);
$query = mysqli_query($con, "SELECT * FROM third LIMIT $limit,3");

3 Comments

$query = mysqli_query($con, "SELECT * FROM third LIMIT " . (3*($page-1)) . ",3"); dosent work. $query = mysqli_query($con, "SELECT * FROM third LIMIT $limit,3"); works great. WTF?
I don't know what "doesn't work" means. I also don't respond well to "WTF".
WTF always using on internet it doesn't mean somebody says bad thingsto you.and thanks for your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.