note 103082 added to function.mysql-connect
| From: | J_S_Fink at yahoo dot com | Date: | Thu, 24 Mar 2011 15:50:25 +0000 |
| Subject: | note 103082 added to function.mysql-connect | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178169@lists.php.net to get a copy of this message | ||
Here's a singleton class to manage a single Database connection. The Open method uses constant
values as defautls, read from an ini file through an initializer script loaded at the top of each
web page. You can also override the default values by manually entering your own (in case you need
to switch servers or database names mid-script).
The qry function takes a prepared statement and will return the first row, first associative row,
first cell, or entire result set based on the second parameter (entire result if omitted).
Usage:
$DB = DB::Open();
$result = $DB->qry(" {SQL Statement} ;");
<?php
abstract class Database_Object
{
protected static $DB_Name;
protected static $DB_Open;
protected static $DB_Conn;
protected function __construct($database, $hostname, $hostport, $username, $password)
{
self::$DB_Name = $database;
self::$DB_Conn = mysql_connect($hostname . ":" . $hostport, $username, $password);
if (!self::$DB_Conn) { die('Critical Stop Error: Database Error<br />' .
mysql_error()); }
mysql_select_db(self::$DB_Name, self::$DB_Conn);
}
private function __clone() {}
public function __destruct()
{
// mysql_close(self::$DB_Conn); <-- commented out due to current shared-link close
'feature'. If left in, causes a warning that this is not a valid link resource.
}
}
final class DB extends Database_Object
{
public static function Open($database = DB_NAME, $hostname = DB_HOST, $hostport = DB_PORT,
$username = DB_USER, $password = DB_PASS)
{
if (!self::$DB_Open)
{
self::$DB_Open = new self($database, $hostname, $hostport, $username, $password);
}
else
{
self::$DB_Open = null;
self::$DB_Open = new self($database, $hostname, $hostport, $username, $password);
}
return self::$DB_Open;
}
public function qry($sql, $return_format = 0)
{
$query = mysql_query($sql, self::$DB_Conn) OR die(mysql_error());
switch ($return_format)
{
case 1:
$query = mysql_fetch_row($query);
return $query;
break;
case 2:
$query = mysql_fetch_array($query);
return $query;
break;
case 3:
$query = mysql_fetch_row($query);
$query = $query[0];
return $query;
default:
return $query;
}
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 96.245.203.216
----
Manual Page -- http://www.php.net/manual/en/function.mysql-connect.php
Edit -- https://master.php.net/note/edit/103082
Del: integrated -- https://master.php.net/note/delete/103082/integrated
Del: useless -- https://master.php.net/note/delete/103082/useless
Del: bad code -- https://master.php.net/note/delete/103082/bad+code
Del: spam -- https://master.php.net/note/delete/103082/spam
Del: non-english -- https://master.php.net/note/delete/103082/non-english
Del: in docs -- https://master.php.net/note/delete/103082/in+docs
Del: other reasons-- https://master.php.net/note/delete/103082
Reject -- https://master.php.net/note/reject/103082
Search -- https://master.php.net/manage/user-notes.php