PDO::getAvailableDrivers

(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 1.0.3)

PDO::getAvailableDrivers 返回一个可用驱动的数组

说明

static PDO::getAvailableDrivers ( ) : array
pdo_drivers ( ) : array

此函数(方法)返回所有当前可用在 PDO::__construct() 的参数 DSN 中的 PDO 驱动。

返回值

PDO::getAvailableDrivers() 返回一个 包含可用 PDO 驱动名字的数组。如果没有可用的驱动,则返回一个空数组。

范例

Example #1 一个 PDO::getAvailableDrivers() 的例子

<?php
print_r
(PDO::getAvailableDrivers());
?>

以上例程的输出类似于:

Array
(
    [0] => mysql
    [1] => sqlite
)

User Contributed Notes

iabdullah 07-Aug-2014 07:31
Since the method is a static, one practice is using it to check whether a specific server database driver is available and configured correctly with PDO before establishing the connection:
<?php
try {
    if (!
in_array("mysql",PDO::getAvailableDrivers(),TRUE))
    {
        throw new
PDOException ("Cannot work without a proper database setting up");
    }
}
catch (
PDOException $pdoEx)
{
    echo
"Database Error .. Details :<br /> {$pdoEx->getMessage()}";
}
?>

or to check for any driver in general:
<?php
   
if (empty(PDO::getAvailableDrivers()))
    {
        throw new
PDOException ("PDO does not support any driver.");
    }
?>
faruk at pamukbilisim dot com 02-Feb-2014 05:13
/*
 * Coder : PamukBilisim
 * Date : 02/02/2014
 */
function getDriverList($ayrac = ",", $echo = true){
 
$ARR_DRIVERS = array();
$CountDrivers = 0;
foreach(PDO::getAvailableDrivers() AS $DRIVERS) :
     
    $CountDrivers++;
    $ARR_DRIVERS[$CountDrivers] = $DRIVERS;
 
endforeach;
 
$_GET_DRIVER_LIST = implode($ayrac, $ARR_DRIVERS);
 
if( $echo ): echo $_GET_DRIVER_LIST; else : return $_GET_DRIVER_LIST; endif;
 
}

Example :

echo "Kullanabilece?iniz pdo veritabanlar? : " . getDriverList(" , ", false);