Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
  • entries
    7
  • comments
    15
  • views
    3,683

MySQL - скрываем ошибки


ArtemPitov

1,800 views

 Share

Давно все жалуются что при ошибках 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();
		}
	}
}

 

  • +1 8
 Share

0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.