note 89507 added to ref.pdo-mysql.connection
| From: | codeslinger at compsalot dot com | Date: | Wed, 11 Mar 2009 15:07:35 +0000 |
| Subject: | note 89507 added to ref.pdo-mysql.connection | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-151739@lists.php.net to get a copy of this message | ||
Here is a function that provides a generic interface to connect
to a mySQL PDO. This takes care of the default values automatically
and deals with the difference between a host name and a socket path.
The advantage of using LOCALHOST for the default is that you don't have
to worry about IPv4 vs IPv6. But keep in mind that mySQL login security
does not treat the name as being interchangeable with the ip address.
You could of course change the default to use a socket path instead.
Enjoy!
Codeslinger
P.S. This forum forces you to limit the length of your lines, so I had to
split a couple of things.
<?php
//pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
//
// Creates a new mySQL database connection
//
// Returns a PDO object or an error message
// Use: is_object() to verify the result
//
//
// If $DBHost starts with a '/' then it is treated as a Socket
//
// if $DBHost is empty it will default to LOCALHOST which is NOT the
// same as 127.0.0.1
//
// $DBPort is optional and will use the standard mySQL default if not
// specified
//
// The database name ($DBName) is optional, but if it is specified
// it must exist or an error results
//
// 2009-03-11 Created: by codeslinger at compsalot.com
//
// Released to the Public Domain free to use and modify
//
//
function dbPDO_Connect_mySQL($DBUser, $DBPass, $DBName = false,
$DBHost = false, $DBPort = false)
{
$DBNameEq = empty($DBName) ? '' : ";dbname=$DBName";
if (empty($DBHost)) $DBHost = 'localhost';
If ($DBHost[0] === '/')
{
$Connection = "unix_socket=$DBHost";
}
else
{
if (empty($DBPort)) $DBPort = 3306;
$Connection = "host=$DBHost;port=$DBPort";
}
//======================
try
{
$dbh = new PDO("mysql:$Connection$DBNameEq"
, $DBUser, $DBPass);
}
catch (PDOException $e)
{
return $e->getMessage();
}
return $dbh;
}
//================================
//================================
//
//Example of use:
//connects to the default (localhost)
$dbh = dbPDO_Connect_mySQL('user', 'password', 'database');
//............................................................
//error handler goes here
if (!is_object($dbh)) trigger_error("Failed to connect to 'database' "
." | Error = $dbh", E_USER_ERROR);
//............................................................
//get a record
$sql = "select * from SomeTable limit 1;";
$dbRS = $dbh->query($sql);
$row = empty($dbRS) ? false : $dbRS->fetch(PDO::FETCH_ASSOC);
if (!empty($dbRS)) $dbRS->closeCursor();
//............................................................
//do something with the data
if (!empty($row)) print_r($row);
else echo "Error? no data found\n";
?>
----
Server IP: 208.69.120.35
Probable Submitter: 67.40.193.68
----
Manual Page -- http://www.php.net/manual/en/ref.pdo-mysql.connection.php
Edit -- https://master.php.net/note/edit/89507
Del: integrated -- https://master.php.net/note/delete/89507/integrated
Del: useless -- https://master.php.net/note/delete/89507/useless
Del: bad code -- https://master.php.net/note/delete/89507/bad+code
Del: spam -- https://master.php.net/note/delete/89507/spam
Del: non-english -- https://master.php.net/note/delete/89507/non-english
Del: in docs -- https://master.php.net/note/delete/89507/in+docs
Del: other reasons-- https://master.php.net/note/delete/89507
Reject -- https://master.php.net/note/reject/89507
Search -- https://master.php.net/manage/user-notes.php