giles
giles

Reputation: 843

Choosing the right MySQL connection for PHP

There seem to be a lot of choices for MySQL connection from PHP. I guess they all offer different feature sets. I just want to run a simple query, and so I'm attracted to the simplicity of mysql_connect(). Is this OK or are there any considerations I'm missing

Thhanks

Upvotes: 0

Views: 77

Answers (3)

gd1
gd1

Reputation: 11403

Even if you are dealing with a single query or a very simple project, use PDO.
It's how DB stuff it's done nowadays and will likely be done in the future. I think that learning the legacy libraries (mysql, mysqli) is not a good deal right now. The learning curve is quite the same, and with PDO you have a basis for doing anything you want (e.g. changing DBMS).

And, even if you choose to use the legacy, DBMS-bound libraries, please don't use mysql, and do use prepared statements (both mysqli and PDO have them). Don't do stuff like:

mysql_query("SELECT * FROM users WHERE username = '$username' AND password = '$password'");

Upvotes: 1

Bv202
Bv202

Reputation: 4044

If you just want to run a simple query, there really is no difference. If you're working on something bigger, use mysqli or PDO instead so you can use it's features. Especially prepared statements is something you really want to use.

I would just forget about the old mysql-library. Mysqli is not harder to use, but it's a big improvement.

Upvotes: 5

amosrivera
amosrivera

Reputation: 26514

Use mysqli instead. It has the same simplicity and it is the improved version of mysql_connect

See documentation here.

Upvotes: 2

Related Questions