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 9221183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:31:20+00:00 2026-06-18T03:31:20+00:00

Possible Duplicate: Can PHP PDO Statements accept the table name as parameter? Im writing

  • 0

Possible Duplicate:
Can PHP PDO Statements accept the table name as parameter?

Im writing a script that allows users to register and login to my site. Im using PDO prepare and execute to prevent SQL injections.

Building the query manually like this DOES work:

$a_query = "SELECT COUNT(*) FROM ". $login_table . " 
WHERE `username` = ". $my_username . "  
AND `password = " . $my_hash ;
$result_1 = $db->prepare($a_query);
$result_1->execute();

But when I try to use prepare correctly like this, it does NOT:

$a_query = "SELECT COUNT(*) FROM :table 
WHERE `username` = :name
AND `password = :pass ;"
$result_1 = $db->prepare($a_query);
$result_1->bindParam(":table", $login_table);
$result_1->bindParam(":name", $my_username);
$result_1->bindParam(":pass", $my_hash);
$result_1->execute();

The error message I get from $result_1->errorInfo[2] reads:

You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax 
to use near ''customerlogin' WHERE `username` = 'guest' AND `password`
= 'qwerty' at line 1

As you can see, prepare() mysteriously slices off the first part of the query before sending it to mysql.

Can anyone explain to me why and how I can fix this?

  • 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-18T03:31:22+00:00Added an answer on June 18, 2026 at 3:31 am

    As you can see, prepare() mysteriously slices off the first part of the query before sending it to
    mysql.

    It does not. It’s just the error message handler which actually does that, trying to display only the relevant part of the query. Usually such an error message means “look at the query right before this part”. So – it points out at the :table in your query.

    Your query does not work because prepared statements do not support identifiers.

    PDO, as any other raw API, is insufficient for any real-life query.
    A developer should use database abstraction library methods in their application code, not raw API methods.

    It will make life much easier and code shorter. For example, your whole code can be made into only 2 lines:

    $sql  = "SELECT COUNT(*) FROM ?n WHERE `username` = ?s AND `password = ?s";
    $data = $db->getOne($sql, $login_table, $my_username, $my_hash);
    

    And it would be safer than your whole screen-long code using raw PDO.

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

Sidebar

Related Questions

Possible Duplicate: get name of current PHP script in include file Can an included
Possible Duplicate: Can anybody suggest the best image resize script in php? I'm still
Possible Duplicate: .htaccess rewrite image file to php script How can I execute a
Possible Duplicate: PHP PDO: Can I bind an array to an IN() condition? I
Possible Duplicate: Can I use a generated variable name in PHP? Am stuck here!
Possible Duplicate: PHP 2-way encryption: I need to store passwords that can be retrieved
Possible Duplicate: PHP PDO: Can I bind an array to an IN() condition? Alright,
Possible Duplicate: How to prevent SQL injection in PHP? I use PDO prepared statements
Possible Duplicate: How can I convert ereg expressions to preg in PHP? I need
Possible Duplicate: How can I only allow certain filetypes on upload in php? i'm

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.