Jump to content
  • entries
    7
  • comments
    10
  • views
    1,239

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

ArtemPitov

719 views

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


0 Comments


Recommended Comments

There are no comments to display.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
You are posting as a guest. If you have an account, please sign in.
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

×

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.