1
votes

I set up a website with database connection and queries using PHP msqli.

Now I am realising prepared statements are best practice, I am looking how to implement them; re-write mysqli database connection code along with database queries in the PDO format OR just use mysqli prepared statements.

  1. I see from PHP API documentation that server-side prepared statements are supported by mysqli but not client-side prepared statements. When would a client-side prepared statement be of use? I understand the sole purpose of prepared statements to be the querying of databases so a server-side language would always be involved and therefore a server-side prepared statement can always be used.

  2. It appears from the below link that prepared statements like below can be used with my mysqli connection.

http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php

    if (!($stmt = $mysqli->prepare("INSERT INTO test(id) VALUES (?)"))) {
    echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
    }

When I look at PDO documentation :id style placeholders are used rather then the ?. Can the below be used with a mysqli connection? Any benefits apart from making things a little easier to read?

   $stmt = $conn->prepare('SELECT * FROM myTable WHERE id = :id');
   $stmt->execute(array('id' => $id));

Should i just forget mysqli and just get up to speed with PDO?

Any guidance would be much appreciated. Looking at how to approach this.

1
PDO can also use ? indexed placeholders. And it provides a much simpler API to bind values. - mario
So PDO binding syntax cannot be used if i made the database connection using mysqli? - DVCITIS
Yes, you cannot. Don't use mysqli unless you need one of the mysql-specific extras it provides (you don't). Everyone else is converging on PDO. - mario

1 Answers

3
votes

You have several questions here. Here is my short answer.

  • Use msqli if you are only using a mysql Database
  • Use PDO if you would like to support many more databases (SQL server, oracle etc...)

Use whichever you like, they both will serve you the same way when it comes to mysql database.

Now as you can see PDO has a clear advantage because you can use the same code to talk to different databases.