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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:08:07+00:00 2026-05-20T11:08:07+00:00

I modified this code from somewhere but I am not sure if I am

  • 0

I modified this code from somewhere but I am not sure if I am doing it correctly,

I use method to insert data into database,

# insert or update data
    public function query($query, $params=array())
    {
        try
        {
            $stmt = $this->connection->prepare($query);
            $params = is_array($params) ? $params : array($params);
            $stmt->execute($params);
            return true;
        }
        catch (PDOException $e) 
        {
            # call the get_error function
            $this->get_error($e);
        }
    }

Then I just need to call it like this,

$sql = "
    INSERT root_countries_cities_towns (
        tcc_names,
        cny_numberic,
        tcc_created
    )VALUES(
        ?,
        ?,
        NOW()
    )";

$pdo->query($sql,array('UK','000'));

It works fine perfectly! but I don’t understand what this line does – can someone explain please?

$params = is_array($params) ? $params : array($params);

I thought I have to use bindParam to bind the parameters first, but it seems that I don;t have to anymore with is method – is it safe and secure then??

Does it meant that I don’t have to prepare the query in this way anymore?

$sql = "
        INSERT root_countries_cities_towns (
            tcc_names,
            cny_numberic,
            tcc_created
        )VALUES(
            :name,
            :numberic,
            NOW()
        )";

and forget about this binding?

$stmt = bindParam(':name','UK', PDO::PARAM_STR);
$stmt = bindParam(':numberic','000', PDO::PARAM_STR);

Thanks.

  • 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-05-20T11:08:08+00:00Added an answer on May 20, 2026 at 11:08 am

    I guess that’s pretty much PHP syntax question rather than PDO one.

    $params = is_array($params) ? $params : array($params);
    

    is a shortland (called ternary operator)) for

    if (is_array($params)) {
       $params = $params;
    } else {
      $params = array($params);
    }
    

    which I’d rather wrote as

    if (!is_array($params)) $params = array($params);
    

    which is pretty self-explanatory and can be read almost in plain English:

    if $params is not an array, let’s make it array with one value of former $params

    That’s why I hate ternary operator (and lambdas) and always avoid it’s use. It makes pretty readable code into a mess. Just out of programmer’s laziness.

    To answer your other questions,

    Does it meant that I don’t have to prepare the query in this way anymore?

    Who said that? You’re preparing it all right in your code, check it again.

    and forget about this binding?

    that’s true. execute($params) is just another way to bind variables.

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

Sidebar

Related Questions

I use comboboxes code taken from jquery examples . This is modified to use
I have this modified code example I took from the wikipedia article on anonymous
I'm not sure how to ask this question, but here goes... I am rendering
I ran across this chunk of code (modified) in our application, and am confused
I request a website header, however, there is not Last-Modified info in this http
I'm currently struggling with this Collection was modified; enumeration operation may not execute issue.
I'm trying out the mapreduce framework from ( http://code.google.com/p/appengine-mapreduce/ ) and modified the demo
I'm javascript newbie...please bear with me. Modified some jQuery animation code from w3c tutorial.
Can someone please help. I have been searching and encountered/modified this code I am
Here's a modified version of code from page 586 of Starting Out With C++

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.