Usando la tua classe non mi si connette, mi dice 1045: Access denied for user 'ODBC'@'localhost' (using password: NO) ![Confused](https://forum.it.altervista.org/images/smilie/icon_confused.gif)
Mancava una parentesi perchè in locale ho altro codice che non serviva qui.
Comunque eccola completamente:
Codice PHP:
<?php
class Database {
private $db_name, $db_user, $db_host, $db_pass;
var $connection;
var $query_result;
function __costruct($db_name, $db_user, $db_host, $db_pass)
{
if($this->connection = mysql_connect($this->db_host, $this->db_user, $this->db_pass))
{
mysql_select_db($this->db_name, $this->connection);
$this->db_security('unset');
}
else
{
$this->db_security('unset');
$this->error(true);
}
}
function close()
{
if($this->connection)
{
@mysql_close($this->connection);
}
}
function query($sql)
{
if(!empty($sql))
{
$result = @mysql_query($sql, $this->connection);
if(!$result)
{
$this->error($query = true);
$result = false;
}
return $result;
}
else
{
trigger_error('Sql empty', E_USER_WARNING);
}
}
function fetch_row(&$sql)
{
@mysql_fetchrow($sql);
}
function error($query = false)
{
global $lang;
$debug = debug_backtrace();
$output = '';
foreach($debug as $number => $trace)
{
if($number > 0)
{
$output .= '<br />';
$output .= '<b>FILE:</b> ' . htmlspecialchars($trace['file']) . '<br />';
$output .= '<b>LINE:</b> ' . ((!empty($trace['line'])) ? $trace['line'] : '') . '<br />';
$output .= '<b>CALL:</b> ' . htmlspecialchars($trace['class'] . $trace['type'] . $trace['function']) . '()<br />';
}
}
$message = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
$message .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="it-it" lang="it-it">';
$message .= '<head><title>' . $lang['ERROR']['GENERAL'] . '</title>';
$message .= '<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" /><meta name="author" content="FedericoBiccheddu" />';
$message .= '<meta name="robots" content="noindex, nofollow" /><link href="/favicon.ico" rel="shortcut icon" type="image/x-icon" />';
$message .= '<style type="text/css"> /* <![CDATA[ */';
$message .= '* { margin: 0; padding: 0; } html { font-size: 100%; height: 100%; margin-bottom: 1px; background-color: #E4EDF0; } body { font-family: "Lucida Grande", Verdana, Helvetica, Arial, sans-serif; color: #536482; background: #E4EDF0; font-size: 62.5%; margin: 0; padding: 15px;} ';
$message .= 'a:link, a:active, a:visited { color: #006699; text-decoration: none; } a:hover { color: #DD6900; text-decoration: underline; } ';
$message .= '#page-footer { clear: both; padding: 5px; font-size: 1em; text-align: center; border-top: 1px solid #CCCCCC; } ';
$message .= '.panel { padding: 10px; margin: 5px 0; background-color: #FFFFFF; border: solid 1px #A9B8C2; } ';
$message .= '#error #content h1 { line-height: 1em; margin-bottom: 0; color: #DF075C; } ';
$message .= '#error #content { color: #333333; font: bold 1.2em "Lucida Grande", Arial, Helvetica, sans-serif; text-decoration: none; text-align: left; } ';
$message .= ' /* ]]> */';
$message .= '</style></head>';
$message .= '<body id="error">';
$message .= '<div id="content" class="panel">';
$message .= '<h1>' . $lang['ERROR']['GENERAL'] . '</h1>';
$message .= '<div>';
$message .= $output . (($sql !== false) ? mysql_errno() . ': ' . mysql_error() : '') . '</div>';
$message .= '<div id="page-footer">© 2009 Federico Biccheddu</div></div></body></html>';
die($message);
}
private function db_security($mode = null)
{
switch($mode)
{
case 'unset':
unset($this->db_name);
unset($this->db_user);
unset($this->db_host);
unset($this->db_pass);
break;
default:
return;
}
}
}
?>
Istanzio ed uso così:
Codice PHP:
$db = new Database('db_name', 'db_root', 'db_host', 'db_pass');
$query = $db->query('SELECT * FROM ' . USERS_TABLE);
Non mi restituisce nessun errore di mysql però