php code to test pdo is available? - php

I want to use PDO but I'm not sure whether my hosting has set it up properly.
How can I test, in PHP, whether it is setup and working for MySQL?

PDO is always installed for php 5.1+. You can check for specific db drivers that are installed or not using phpinfo(); You could try to check for specific drivers using #Mark Baker idea and checking for specific constants;
var_dump(defined(PDO::MYSQL_ATTR_LOCAL_INFILE)); // mysql
var_dump(PDO::FB_ATTR_TIME_FORMAT)); // firebird
Note that not all drivers have specific constants defined so phpinfo() remains best solution.
Using command line you can check using:
$ php -m
As an alternative of phpinfo() you can use:
extension_loaded ('PDO' ); // returns boolean
// or
extension_loaded('pdo_mysql');
// or get all extensions and search for a specific one
get_loaded_extensions();

Aside from using phpinfo() to see if it's correctly listed
if (!defined('PDO::ATTR_DRIVER_NAME')) {
echo 'PDO unavailable';
}

Try this
print_r(PDO::getAvailableDrivers());
should give applications supported by PHP
Array ( [0] => mysql [1] => sqlite )

Using command line, for PDO:
php -m|grep -i pdo
For PDO with MySQL support:
php -m|grep -i pdo_mysql
To install php mysql support, search for the package name (Ubuntu):
apt-cache search php5*|grep mysql
And install it if not already did (Ubuntu):
sudo apt-get install php5-mysql

Create a file that contains
<?php
phpinfo();
?>
It will show you if PDO (and other features are enabled)

For Windows servers, I've found the following useful for checking which PDO extensions are loaded from the command prompt:
php -m|findstr -i pdo_
On my system, that command nets me the following results:
pdo_mysql
PDO_ODBC
pdo_pgsql
pdo_sqlite

Related

Trying to add the firebird extension to PDO with no luck. I have a RHEL7 server

I've been trying to add the Firebird extension to PDO with no luck. I have a RHEL7 server, I ran ./configure --with-firebird successfully, ran the make command, and make install successfully.
If I type
php -i | grep PDO
I get the following:
PDO
PDO support => enabled
PDO drivers => firebird, sqlite
PDO_Firebird
PDO Driver for Firebird/InterBase => enabled
PDO Driver for SQLite 3.x => enabled
However, when going to my site's phptest.php file it only shows
mysql
sqlite
I guess I'm missing one last step, but I don't know what.
Try running slightly different command
./configure --with-firebird --with-pdo-firebird
Then find phph.ini (or the file that acts like that in Linux platform) and enable (de-comment?) the line like extension=php_pdo_firebird.dll ( in Linux it probably would be like libphp_pdo_firebird.so or something, see for the specific files your building process generate).
See also:
http://php.net/manual/en/ref.pdo-firebird.php
Enable PHP Firebird connection

Enabling PDO_OCI in PHP

I have been using oci_ in PHP but I would like to start using PDO.
I have read http://www.php.net/manual/en/ref.pdo-oci.php but I'm not sure how to enable PDO_OCI. I see the command listed on the page but I installed PHP using sudo apt-get install php5 and not via the source.
<?php
print_r(PDO::getAvailableDrivers());
?>
shows
Array
(
[0] => mysql
)
Can someone help as the PHP website does not have much information other than that single command? I understand it is experimental but I would like to try it, I just can't find much information on it.
Install the PDO_OCI drivers using pecl. This tutorial may help: Installing PHP & Oracle PDO Drivers on Ubuntu

(Linux) PDO cannot find the MySQL driver

So basically I have been always developing with PHP under Windows with the WAMP package and I never had any trouble.
Now, I am trying to transition to Linux (Mint). Everything is well, except for the PDO extension which cannot find the MySQL driver. In fact, it states not having any available drivers at all.
First, I installed Apache:
sudo apt-get install apache2
Then, PHP:
sudo apt-get install php5
MySQL for PHP (I am not exactly sure this was required):
sudo apt-get install php5-mysql
Finally, the command line interface for PHP:
sudo apt-get install php5-cli
Note that I had installed MySQL a while ago for other purposes.
Everything else is working well. The server starts, runs, executes scripts. I am also able to create an instance of PDO, but then it throws a PDOException stating that it cannot find the MySQL driver. The code is typical of a PDO initialization :
$pdo = new PDO("mysql:host=127.0.0.1;db=testdb", "root", "");
I looked at the phpinfo() and for the value PDO I see no drivers. However, I see the line MySQL driver for PDO followed by the names of its authors, but I cannot tell if it guarantees the presence of the driver. Notice that I also see the authors of all the other drivers (SQLite, PostgreSQL, Oracle, etc.).
The command php -m (which, according to the php --help, lists all modules compiled with PHP) also clearly indicates that both PDO and pdo_mysql are installed.
My search returned a lot of results about the extensions pdo.so and pdo-mysql.so. I have been trying to reference them into the php.ini file without success. It would probably have a higher chance of success if I actually had these files, but so far I have been unable to find them.
So, here we are, about five hours later, with me being completely stuck.
Thank you.

How can I tell if PDO module installed/disabled?

I'm developing a PHP application. I just moved a bunch of files over to the production server, to discover that PDO is apparently not installed (Fatal error: Class 'PDO' not found in [file] on line [line]). The prod server is using PHP 5.3.6; according to the documentation I've found, "PDO and the PDO_SQLITE driver is enabled by default as of PHP 5.1.0".
I have very little experience administering PHP. How do I tell if this module is installed but disabled, or absent altogether? And what do I need to do to get it running?
Generally you can just do phpinfo(); to find out what modules are installed.
Or from the commandline:
php -m
Additionally you could use: class_exists('PDO') to find out whether the PDO class indeed is accessible.
In the terminal type:
php -i |grep PDO
The -i is used to display the output of phpinfo().
The |grep will filter the list to the ones containing PDO.

Configure PHP and mysql connection under linux

I encounter this problem when configure PHP and mysql on my linux box (Fedora 14).
The problem is when I invoke mysqli_connect(), an error issues:
Call to undefined function mysqli_connect().
while call to mysql_connect() works fine.
I install PHP and mysql manually and also turn on the flag --with-mysql when installing PHP. Can't figure out where things go wrong.
Mysql-i is not Mysql, these are 2 distinct PHP modules and MySQL APIs. Have a look at PHP's documentation regarding the mysqli installation.
If yum is an option for you, you can simply run the following to install the mysqli extenstion:
yum install php-mysqli
EDIT: Marc B informs that mysqli is included in the core php rpm on Fedora 14. Simply install php via yum (after removing your manual install) with yum install php
http://php.net/manual/en/mysqli.installation.php says:
To use MySQL Native Driver with mysqli you need to configure the PHP source code using the --with-mysqli=mysqlnd option, prior to building PHP.
In other words, --with-mysql is for a different module (which, as you note, works fine).

Categories