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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:59:41+00:00 2026-06-14T03:59:41+00:00

i am trying to write a function in php and mysql to select values

  • 0

i am trying to write a function in php and mysql to select values from PHP and mysql using PDO

function getRec($id=0)
{
    ($id==0?$addQuery="":$addQuery=" where id =".$id);
    $statement = $dbh->prepare("select * from TCMS :name order by id");
    $statement->execute(array(':name' => $addQuery));
    $row = $statement->fetchAll(); 
    return $row ;
} 

i got error

Fatal error: Uncaught exception ‘PDOException’ with message
‘SQLSTATE[42000]: Syntax error or access violation: 1064 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 ” where id =2′
order by id’ at line 1′ in /Applications/XAMPP/xamppfiles/htdoc

actually what i am trying

if value (2) of ID is passed then statement will be

select * from TCMS where id=2 order by id

And if ID=0 then select statement will be

select * from TCMS order by id

i am new to PDO and not sure of exact syntax.

how to do 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-14T03:59:42+00:00Added an answer on June 14, 2026 at 3:59 am

    Do this instead:

    function getRec($id=0)
    {
        //($id==0?$addQuery="":$addQuery=" where id =".$id);
        if ($id == 0)
        {
            $statement = $dbh->prepare("select * from TCMS order by id");
            $statement->execute();
        }
        else
        {
            // Notice the SQL string has changed. Placeholder :name now properly takes the place of a SQL value.
            $statement = $dbh->prepare("select * from TCMS where id = :name order by id");
            $statement->execute(array(':name' => $id));
        }
    
        $row = $statement->fetchAll(); 
        return $row ;
    }
    

    What you’re doing wrong is you’re attempting to bind and execute the SQL with the placeholder as arbitrary string values, which is not what the placeholder is for.

    The placeholder is to be set in the place of the value (not table names or anything else) so that the value when passed in during execution will be properly handled by PDO internally for the correct escaping.

    The function I wrote should help to create valid SQL.

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

Sidebar

Related Questions

I am trying to write a php function to stop MySQL injection attempts. What
I am trying to write a PHP function which gets the sum of values
Am trying to write a new record using the jquery function. $.post(insertuser.php,$(#rohanStart).serialize(),function(data){ alert(data); });
I'm trying to write a php function that gets all the basic food data.
I am trying to write a function that includes a file named header.php in
I am new to php and trying to write a login function. Bit stuck
I'm trying to change some php code so that i get my values from
I'm currently trying to write a simple ORM with PHP and mysql. I want
I am trying to write a php function that goes to my database and
I am trying to figure out the best way to write a PHP function

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.