Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8626443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:02:12+00:00 2026-06-12T08:02:12+00:00

So I have been using prepared statements for a while and for a number

  • 0

So I have been using prepared statements for a while and for a number of projects and it has been a really good clean way to interact with the MySQL db, but today I have come across a strange problems.

My prepared statement has started adding extra ‘ to the sql statements and for the life of me I have no idea why…

so here is the code:

<?php

    $sortby="ORDER BY submit_date DESC";
    $offset = 3;

    $sql = "SELECT img_id, img_name, submit_date FROM tbl_images WHERE img_active='y' :sortby LIMIT :offset, 9";

    $stmt = $this->_db->prepare($sql);
    $stmt->bindParam(":sortby", $sortby, PDO::PARAM_STR);
    $stmt->bindParam(":offset", $offset, PDO::PARAM_INT);
    $stmt->execute();

?>

so the above doesnt return anything, so looking at the database logs, this is what the query looks like

SELECT img_id, img_name, submit_date FROM tbl_images WHERE img_active='y' 'ORDER BY submit_date DESC' LIMIT 3, 9

it seems to have put an extra set of ‘ ‘ around the “ORDER BY submit_date DESC”, but yet hasnt around the offset?

Can anyone spot the problem as its driving me mad 🙂

Thank you in advance!

Solution, thanks to the guys that posted, you were correct, I split the fields out to parts and works like a charm. Code solution below:

<?php

    $sortfield="submit_date";
    $sortway="DESC"
    $offset = 3;

    $sql = "SELECT img_id, img_name, submit_date FROM tbl_images WHERE img_active='y' ORDER BY :sortfield :sortway LIMIT :offset, 9";

    $stmt = $this->_db->prepare($sql);
    $stmt->bindParam(":sortfield", $sortfield, PDO::PARAM_STR);
    $stmt->bindParam(":sortway", $sortway, PDO::PARAM_STR);
    $stmt->bindParam(":offset", $offset, PDO::PARAM_INT);
    $stmt->execute();

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:02:13+00:00Added an answer on June 12, 2026 at 8:02 am

    Have a look at the documentation for mysqli_stmt::prepare:

    The markers are legal only in certain places in SQL statements. For example, they are allowed in the VALUES() list of an INSERT statement (to specify column values for a row), or in a comparison with a column in a WHERE clause to specify a comparison value.

    Basically, anything structural to the query is not allowed to be a bound parameter. Only data can be sent in this way.

    PDO’s prepared statements work in effectively the same way. In your case, however, PDO is a bit stupid, because it’s running in “emulate prepares” mode (which is the default, but you should turn it off to get the most from PDO). It basically does all the substitution itself, rather than sending the query and the data to the server separately. It sees that the data is a string and thinks “aha, a string: I need to put quotes around this.” You therefore end up with your malformed query.

    The solution is not to build up structural parts of your query with bound parameters. Either substitute them in yourself with concatenation, or (and this is better) have alternative query strings for different settings. This is the most secure way: anything involving concatenation is a recipe for insecurity.

    Oh, and turn PDO emulate prepares off!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just getting into MySql/MySqli really, and I'm using prepared statements. The whole of
I have been using TortoiseSVN for some time and I really like it. I
I have been using CI just fine using the MySQL driver. I want to
I have been spotting the sentence PHP PDO's prepared statements prevents SQL injection .
I am familiar with using PHP to perform mySQL queries. However, I have been
I am totally confused by mySQLi. Although I have been using procedural mysql calls
Much have been written about the benefits of using PDO::prepare , but little has
I have been using Stanford POS Tagger to tag parts of speech in a
I have been using play 1.2.5rc4 for development of one app and I have
I have been using an API to do some work. This is how I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.