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

  • Home
  • SEARCH
  • 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 8441465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:34:01+00:00 2026-06-10T08:34:01+00:00

$user=’root’; try{ $pdo=new PDO(‘mysql:host=localhost;dbname=test’,$user); $pdo->exec(‘set character set utf8’); } catch(PDOException $e){ echo ‘Error: ‘.$e->getMessage();

  • 0
$user='root';

try{
$pdo=new PDO('mysql:host=localhost;dbname=test',$user);
$pdo->exec('set character set utf8');
}
catch(PDOException $e){
echo 'Error: '.$e->getMessage();
}

//using bound variables?
$stmt=$pdo->prepare('insert into test(name) value(:name)');
$stmt->bindParam(':name',$_POST['name']);
$stmt->execute();

//using named parameters
$stmt=$pdo->prepare('insert into test(name) value(:name)');
$stmt->execute(array(':name'=>$_POST['name']));

//using placeholders
$stmt=$pdo->prepare('insert into test(name) value(?)');
$stmt->execute(array($_POST['name']));

//using bound parameters w/ placeholders
$stmt=$pdo->prepare('insert into test(name) value(?)');
$stmt->bindParam($_POST['name']);
$stmt->execute();

I just want to know what is the best to use for my as a PHP starter, I’m just confused what to use and I want to know what is the best and commonly use.

  • 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-10T08:34:02+00:00Added an answer on June 10, 2026 at 8:34 am

    I find the second method the best:

    //using named parameters
    $stmt=$pdo->prepare('insert into test(name) value(:name)');
    $stmt->execute(array(':name'=>$_POST['name']));
    

    You have named params, so you know what is what in the array – I hate looking at five or more ? in the code and trying to work out if the order is right, and it takes less lines of code to throw in an array and execute it all at the same time than going through many ->bindParam() lines.

    If this option was off the table though, I would go through the many lines of ->bindParam() over the others.

    This question comes down to personal preference. Choose what is best for you, what is easy to read and understand (especially if you come back to it weeks or months later) and what is easy to read for others who might have to pick through your code to debug it.

    Edit: Nothing works faster as such. Putting the query together might take milliseconds longer one way or the other, but the queries will take the same amount of time to execute on the database. Faster really isn’t something to look at here.

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

Sidebar

Related Questions

User may input character like Ā Ē ú, how to remove that mark and
User has pets , pets may have pictures One picture may be set as
User inserts new records in the table in his browser with no postback (using
User is a nested attribute of Announcement . When a new announcement is created,
@user = find_user @user_sport = UserSport.new(params[:iuser_sport]) @user.user_sports << @user_sport What exactly last line of
User comes to checkout and selects register as new customer, when placing the order,
User of my site upload images which are used as avatars. I have set
[user@host][~] 6> cc -xO4 timing.c -o timing [user@host][~] 6> ./timing Total run time: 0
User Entity class User{ int id; @OneToMany Set<Role> roles; } :User class have so
@user = User.new @user.id returns nil but i need to know it before 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.