PHP Code to Test Pdo Is Available

php code to test pdo is available?

Aside from using phpinfo() to see if it's correctly listed

if (!defined('PDO::ATTR_DRIVER_NAME')) {
echo 'PDO unavailable';
}

How to determine if PDO is enabled in PHP?

Check if the class exists:

if (class_exists('PDO'))

I appreciate the support and all the upvotes I still get, but please check Salman Abbas's answer for the proper way to do this.

PDO Connection Test

you need to set the error mode when connection to the database:

try{
$dbh = new pdo( 'mysql:host=127.0.0.1:3308;dbname=axpdb',
'admin',
'1234',
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
die(json_encode(array('outcome' => true)));
}
catch(PDOException $ex){
die(json_encode(array('outcome' => false, 'message' => 'Unable to connect')));
}

for more infos see the following links:

Using MySQL with PDO

Errors and error handling

Testing coverage with PHPUnit and PDO

This is exactly what PHPUnit's test doubles are for. And kudos for designing your classes in an object oriented way, so it's easy to inject mocks into them.

<?php

use PHPUnit\Framework\TestCase;

class DBAccessTest extends TestCase
{
public function test_something_goes_wrong()
{
// Create test doubles.
$stmtMock = $this->createMock(\PDOStatement::class);
$pdoMock = $this->createMock(\PDO::class);

// Configure the stubs.
$stmtMock->method('execute')
->willReturn(false);
$pdoMock->method('prepare')
->willReturn($stmtMock);

// Inject the mock.
$test = new DBAccess($pdoMock);

// Assert.
$this->assertFalse($test->select());
}
}

How can I check PDO connection?

It depends on what you set for PDO::ATTR_ERRMODE. In your case, it's set as PDO::ERRMODE_EXCEPTION, so an error will throw an Exception.

$dbh_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

This error mode however is not applied when you connect (i.e., when __constructor() is called in PDO), since a PDOException is always thrown:

PDO::__construct() will always throw a PDOException if the connection fails regardless of which PDO::ATTR_ERRMODE is currently set. Uncaught Exceptions are fatal.

You can read about the different modes here.

How can I tell if PDO module installed/disabled?

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.

PDO test if connected

In a single click from this question, in the PDO tag wiki lies the exact how-to:

$dsn = "mysql:host=localhost;dbname=test;charset=utf8";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO($dsn,'root','', $opt);

As well as a warning

DO NOT use try..catch operator just to handle an error message.

Uncaught exception already excellent for this purpose, as it will treat PDO errors just the same way as other PHP errors - so, you can define the behavior using site-wide settings.
A custom exception handler could be added later, but not required. Especially for new users, it is recommended to use unhandled exceptions, as they are extremely informative, helpful and secure.
More info...

How to check in PHP if more PDO parameters provided than needed

You're right, that situation doesn't seem to throw exceptions nor trigger errors. PDOStatement::execute() at least returns false so you can roll your own:

$dsn = "mysql:host=$dbhost;dbname=$dbname;charset=utf8mb4";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
$pdo = new PDO($dsn, $dbuser, $dbpass, $options);

$sql = 'SELECT ? AS foo';
$stmt = $pdo->prepare($sql);

if (!$stmt->execute([1, 2])) {
throw new InvalidArgumentException('Failed to execute statement');
}
while ($row = $stmt->fetch()) {
var_dump($row);
}

Not ideal but...



Related Topics



Leave a reply



Submit