Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PHP CONSTANTS in PDO calls

Tags:

php

<?php # Nettuts Tutorial using PHP Data Objects (PDO),

/**This file contains the database access information
 *This file also establishes a connection to mySQL
 *and selects the database.
 *Set the database access information as constants:
 **/
// print_r(PDO::getAvailableDrivers());

DEFINE('DB_USER', 'root');
DEFINE('DB_PASSWORD', 'root');
DEFINE('DB_HOST', 'localhost');
DEFINE('DB_NAME', 'sitename');

$php = "htmlspecialchars";
try {
    #MySQL with PDO_MYSQL
    // $DBH = new PDO("mysql:host={$php(DB_HOST)}; dbname={$php(DB_NAME)}", root, root};  
    $DBH = new PDO("mysql:host=localhost; dbname= sitename", root, root);

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

    # UH-OH! Typed DELECT instead of SELECT!
    $DBH->prepare('DELECT name FROM people');
} catch (PDOException $e) {
    echo "I'm sorry, Dave. I'm afraid I can't do that.";
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}
?>

The Console in OS X returns "[14-Aug-2011 15:59:59] PHP Notice: Use of undefined constant root - assumed 'root' in /Applications/MAMP/htdocs3/nettuts/PHP/PDO for Database Access/mysql_pdo_connect.php on line 20."

I've "googled" and found a partial answer here. So I was hoping for completion here.

TIA

like image 808
Wasabi Avatar asked Aug 14 '11 23:08

Wasabi


People also ask

Is PHP PDO deprecated?

PHP 8.1 deprecates the Serializable interface, and part of this change, PHP 8.1 also deprecates the PDO::FETCH_SERIALIZE functionality. PDO::FETCH_SERIALIZE flag is meant to be used as a flag for PDO statement fetch methods, and if was used, PDO automatically calls unserialize on the data fetched from the database.

What is PDO :: PARAM_STR?

PDO::PARAM_STR. Represents SQL character data types. For an INOUT parameter, use the bitwise OR operator to append PDO::PARAM_INPUT_OUTPUT to the type of data being bound. Set the fourth parameter, length , to the maximum expected length of the output value.

How does PHP PDO work?

PDO—PHP Data Objects—are a database access layer providing a uniform method of access to multiple databases. It doesn't account for database-specific syntax, but can allow for the process of switching databases and platforms to be fairly painless, simply by switching the connection string in many instances.

What PHP PDO function do you use to get your PDO object ready to run a query?

PDO::query() prepares and executes an SQL statement in a single function call, returning the statement as a PDOStatement object.


1 Answers

You do:

$DBH = new PDO("mysql:host=localhost; dbname= sitename", root, root); 

Which should have been:

$DBH = new PDO("mysql:host=localhost; dbname= sitename", 'root', 'root'); 

With quotes. Otherwise PHP thinks it is some constant instead of a string. However by looking at your code I see you have defined constants to access the database so why don't you simply do:

$DBH = new PDO('mysql:host='.DB_HOST.'; dbname='.DB_NAME, DB_USER, DB_PASSWORD); 

UPDATE

I also see that you are using MySQL with PDO. Please note that in order to safely use MySQL with PDO you have to disable emulated prepared statements:

$DBH = new PDO('mysql:host='.DB_HOST.'; dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASSWORD);
$DBH->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

Please note that I have also set the encoding in the DSN string (in my case to utf8). I also wonder if you really need the constants in your code, because often you will only need one connection per request (to the same database) so you don't need to have those globals floating around if you just create the connection once and pass the connection to the parts of the code that need it. See also my somewhat related question for more information about this.

like image 114
PeeHaa Avatar answered Sep 23 '22 15:09

PeeHaa