Давно все жалуются что при ошибках MySQL получаем пароли, набросал вот такую обертку, не претендую на уникальность, если у Вас есть замечания или предложения, всегда welcom =)
mysqli.php
<?php
namespace DB;
final class MySQLi
{
private $connection;
public function __construct( $hostname, $username, $password, $database, $port = '3306' )
{
try
{
\mysqli_report( MYSQLI_REPORT_STRICT );
$this->connection = new \mysqli( $hostname, $username, $password, $database, $port );
$this->connection->set_charset( "utf8" );
$this->connection->query( "SET SQL_MODE = ''" );
}
catch ( \mysqli_sql_exception $e )
{
if ( \defined( 'IS_DEV' ) && IS_DEV )
{
throw $e;
}
}
}
public function query( $sql )
{
try
{
if ( !\is_null($this->connection) )
{
$query = $this->connection->query( $sql );
}
}
catch ( \mysqli_sql_exception $e )
{
if ( \defined( 'IS_DEV' ) && IS_DEV )
{
throw $e;
}
}
if ( !\is_null($this->connection) && !$this->connection->errno )
{
if ( $query instanceof \mysqli_result )
{
$data = array();
while ( $row = $query->fetch_assoc() )
{
$data[] = $row;
}
$result = new \stdClass();
$result->num_rows = $query->num_rows;
$result->row = isset($data[0]) ? $data[0] : array();
$result->rows = $data;
$query->close();
return $result;
}
return true;
}
}
public function escape($value)
{
if ( !\is_null($this->connection) )
{
return $this->connection->real_escape_string($value);
}
return false;
}
public function countAffected() {
if ( !\is_null($this->connection) )
{
return $this->connection->affected_rows;
}
return false;
}
public function getLastId()
{
if ( !\is_null($this->connection) )
{
return $this->connection->insert_id;
}
return false;
}
public function connected()
{
if ( !\is_null($this->connection) )
{
return $this->connection->ping();
}
return false;
}
public function __destruct()
{
if ( !\is_null($this->connection) )
{
$this->connection->close();
}
}
}