What is PHP Data Objects (PDO)? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am relatively new to PHP. I have written some scripts using Oracle and MySQL as databases, using oci_ and mysqli_ but now I am trying to understand what exactly PDO is.
I have read the PDO documentation at http://www.php.net/manual/en/book.pdo.php but still I have a few questions that I'm not sure on.
If I wanted to write a script to connect to an Oracle database I could use oci_ functions, and then if I wanted to use the same script to connect to a MySQL database I would have to convert the script to use mysqli_ functions.
Is the point of PDO that the same script can be used for any database, and the only part that would need to be changed is the DSN?

Is the point of PDO that the same script can be used for any database,
and the only part that would need to be changed is the DSN?
From the manual: Introduction http://www.php.net/manual/en/intro.pdo.php
The PHP Data Objects (PDO) extension defines a lightweight, consistent
interface for accessing databases in PHP. Each database driver that
implements the PDO interface can expose database-specific features as
regular extension functions. Note that you cannot perform any database
functions using the PDO extension by itself; you must use a
database-specific PDO driver to access a database server.
PDO provides a data-access abstraction layer, which means that,
regardless of which database you're using, you use the same functions
to issue queries and fetch data. PDO does not provide a database
abstraction; it doesn't rewrite SQL or emulate missing features. You
should use a full-blown abstraction layer if you need that facility.

Related

Building an application independent of the SQL engine used [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I wanted to ask that if and how is it possible to make a PHP-based application independent of the SQL engine used?
(Under that I mean that the application can be executed on PostgreSQL, MySQL and maybe also Oracle SQL.)
Current idea is to leave the PDO DSN into the config file, so that the queries etc everything else stay the same, but can I face some problems in regards of CRUD actions to the tables themselves?
If I don't add any complex stuff in it, then is it possible for me to make it so that it doesn't depend on the engine at all, only has the PDO DSN for the database and that it can query everything the same way on each of the SQL engines?
Best regards!
Read about Database Abstraction Layer. There are quite some libraries available for PHP. If you choose any of the popular frameworks, to mention Symfony or Laravel - you will have the DBAL out of the box - either Doctrine or Eloquent. Both offer similar query building functionality, IMO based on HQL.
Definitely don't try to write it "your way". Even if you do only simple queries the gramma is different, to mention just the types, incrementing etc...
If you stick to a common subset of SQL which is supported by all your targeted databases, then yes, you can get away with using the same PDO instance, the same queries, and just switching the PDO DSN.
Practically speaking however, for anything but the most trivial queries, you'll probably be using some database specific features which you'll have to implement slightly differently for different databases. Simply speaking that means your code will have to execute query A if connected to MySQL but a slightly different query B if connected to Postgres.
You certainly do not want to implement that using a lot of if..else, instead you want to be using database specific adapters/drivers. In your business code you'll be calling $database->getUserRecords(), and depending on whether $database uses the MySQL or Postgres adapter the query will be slightly different. (Also see dependency injection.)
You can either do this by implementing those adapters by hand and tweaking the queries for each database, or use a more abstracted ORM/DBAL which can assemble the correct query on the fly using database-specific query builders. There are many existing libraries for that.

Best driver for PHP database [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I was just wondering the asnwer to this question..
for a few select, insert, and update querys with only a mysql database which driver is the best? MySQL, MySQLi or PDO for speed?
Don't use the mysql_* functions, that API is depreciated.
MYSQLi is a replacement for MYSQL and also comes with object-oriented and procedural versions.
PDO (PHP Data Objects) is fairly advanced and is a general database abstraction layer with support for MYSQL among other databases.
How to learn PHP, PDO and MYSQLi
Documentation for programming languages
Stack Overflow community
Programming Books
YouTube Channels
Code Course is an excellent resource to learn PHP. Try his website and YouTube Channel.
Good luck! :)
UPDATE: Comparing the Speeds
MYSQLi is slightly faster than PDO and makes programming easy if your familiar with the *mysql_** extension
PDO is not as fast as MYSQLi. However, it offers much easier transition to other databases. It also offers more features (eg. named parameters in prepared statements, object mapping).
The MySQL extension is essentially retired (as of PHP 5.5 or thereabout). Thus, in my opinion, since you are using a MySQL db, you have your choice from the other two (MySQLi supports MySQL only, PDO is MySQL plus a dozen other DB flavors).
Given what you are doing, I don't believe there is going to be significant difference in performance. Some benchmarks indicate that MySQLi is a smidge faster than PDO (depending on what you're doing), so if you really need to squeeze for every millisecond, I'd probably go for MySQLi.

Databasing in Ruby [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm making a website for a group that needs a database. The data is going to store user information among other things. I already know PHP and could easily do it in PHP, but to further myself as a developer, I'd like to learn a language like Ruby. I know how to use databases in PHP. But, when writing Ruby, can I use SQL databases?
We don't know how you call your database in PHP.
Many PHP users use MySQL and call sql statements with the embedded mysql driver,
and then iterate over the results.
You can do this also in Ruby. The mysql driver is not embedded, But you can easily install it with RubyGems. You need the mysql2 gem.
https://github.com/brianmario/mysql2
But if want to be more object-oriented, there is the framework "Ruby On Rails" with "ActiveRecord" for database connection. Here you don't write SQL directly, instead you specify what objects you want to have or store (except in rare edge cases, where you still can write sql)
This needs some learning time. But then it is lot less coding, code is better readable, and security errors like are also easier to avoid.
The basic answer is yes - you can do something like that using Ruby and a framework like ActiveRecord or Sequel, but this far too broad for StackOverflow.
Good afternoon.
Depends on the speed you need .
1) If you need quickly - write on PHP
2) If you have some time and want learn Ruby On Rails, ActiveRecord etc. ... buy some book for beginner, read doc and step by step create application

Should a PDO script written for MySQL work with Oracle? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I understand that in general PDO scripts are cross compatible i.e. generally changing the connection string should work.
In the past I've spent hours searching online after changing a PDO script connection string from MySQL to SQLite as this isn't the case, some things don't work the same (I remember an issue with row counting or something).
So should changing from MySQL to Oracle be generally simple, or are there things to watch out for as in the SQLite case?
So should changing from MySQL to Oracle be generally simple, or are there things to watch out for as in the SQLite case?
There are things to watch out.
More seriously, beside basic SQL query, each RDBMS has its own set of specific features that have to be taken into account. Just to give one example, if you want to limit the result set to one row only, MySQL provides the LIMIT clause. But for Oracle up to 11g, you need a sub-query for that purpose.
If you really need cross-vendor support, you probably should take a look at some library providing database abstraction layer whose job is to allow you to write database-agnostic code. PDO isn't such a library. But Doctrine DAL, Zend_db and many other are.
It is now considered as off-topic to request suggestions for a tool here, but take a look at this old question if you need few pointers: Best PHP DAL (data abstraction layer) so far

PHP PDO vs normal mysqli speed performance benchmark [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
i m working on a project about social networking website where speed optimization is very critical.
is PDO is FASTER ?
i am thinking to switch to PDO is it recommended for use PDO for such a site ?
I doubt using PDO or MySQLi will be bottleneck, though, if you ask for bechmarks, here they are (nothing serious, just couple of tests).
In general, using one or another is a matter of taste, and bottlenecks usually are somewhere else (e.g., queries, indexes, PHP code etc).
One thing you might consider is using some DB wrapper, i.e., class that uses either PDO or MySQLi, whichever you prefer. In your code, use this wrapper instead of using PDO or MySQLi directly. If you do this, you'll be able to switch between PDO, MySQLi or any other library by changing single class instead of changing all the code.
I did a mini benchmark on this a while back. Conclusion. PDO and MySQLi are very similar but the features in PDO are worth using.
http://cznp.com/blog/2/apples-and-oranges-mysqli-and-pdo

Categories