Версия php 5.6
Когда человек мне писал скрипт, то тогда ещё не было таких мыслей, что проект будет развиваться, поэтому первые скрипты коннектились не по фэншую, а с помощью вот этого класса.
DB.php (+/-)
<?php
/**
* @author Maxim Masalov
* @file DB.php
* @created 04.03.2015 16:58
*/
/**
* Class for using database
* Class DB
*/
class DB
{
// Данные для подключения
private $db_config = array
(
'dbHost' => 'localhost',
'dbName' => 'name_base',
'dbUser' => 'admin_base',
'dbPass' => 'pass_base
);
/**
* @var object - Singleton
*/
protected static $_instance;
/**
* Query counter
* @var int
*/
public $query_count = 0;
/**
* Errors
* @var array
*/
public $errors = array();
private $connect;
/**
* Class constructor
*/
private function __construct()
{
$db_host = isset($this->db_config['HOST']) ? $this->db_config['HOST'] : 'localhost';
$db_user = isset($this->db_config['USER']) ? $this->db_config['USER'] : '';
$db_pass = isset($this->db_config['PASSWORD']) ? $this->db_config['PASSWORD'] : '';
$db_name = isset($this->db_config['DATABASE']) ? $this->db_config['DATABASE'] : '';
$this->connect = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($this->connect->connect_errno) {
echo "Error: cannot connect to database server: " . $this->connect->connect_error;
}
$this->query("SET NAMES 'utf8'");
}
/**
* Close access to functions outside of the class
*/
private function __clone()
{
}
/**
* This is singleton pattern
*
* @return DB
*/
public static function getInstance()
{
// Check actuality instance
if (null === self::$_instance) {
// Create new instance
self::$_instance = new self();
}
// return instance
return self::$_instance;
}
/**
* This method send query to database
*
* @param $sql - Query string
* @return resource - result
*/
public function query($sql)
{
$this->query_count++;
return $this->connect->query($sql);
}
/**
* This method insert data to database
*
* @param $table - Table to insert
* @param $fields - Fields to insert
* @return bool|string
*/
public function insert($table, $fields)
{
if (empty($table)) {
return false;
}
if (!empty($fields) AND is_array($fields)) {
$arSet = array();
foreach ($fields as $field => $value) {
if (strlen($value) <= 0) {
$arSet[] = "`" . $field . "` = ''";
} else {
$arSet[] = "`" . $field . "` = '" . $this->toSql($value) . "'";
}
}
$sql = 'INSERT INTO `' . $table . '` SET ' . implode(', ', $arSet);
$return = $this->query($sql);
return $return;
}
return false;
}
/**
* Return last inserted ID
* @return int
*/
public function lastID()
{
return $this->connect->insert_id;
}
/**
* This method update the string in database
*
* @param $table - Table to insert
* @param $fields - Fields to insert
* @param $where - Where string
* @return bool|string
*/
public function update($table, $fields, $where = '')
{
if (empty($table)) {
return false;
}
if (!empty($fields) AND is_array($fields)) {
$arSet = array();
foreach ($fields as $field => $value) {
if (strlen($value) <= 0) {
$arSet[] = "`" . $field . "` = ''";
} else {
$arSet[] = "`" . $field . "` = '" . $this->toSql($value) . "'";
}
}
$sql = 'UPDATE `' . $table . '` SET ' . implode(', ', $arSet);
if (!empty($where)) {
$sql .= ' ' . $where;
}
$return = $this->query($sql);
return $return;
}
return false;
}
/**
* This method executes query to db
*
* @param $table - Table name
* @param array $fields - array fields for select
* @param string $where - the conditions selection
* @param string $limit - Limit
* @return bool|resource
*/
public function getList($table, $fields = array(), $where = '', $limit = '')
{
if (empty($table)) {
return false;
}
$sql = 'SELECT ' . ((!empty($fields) AND is_array($fields)) ? implode(', ', $fields) : '*') . ' FROM ' . $table;
if (!empty($where)) {
$sql = $sql . ' ' . $where;
}
if (!empty($limit)) {
$sql = $sql . ' ' . $limit;
}
return $this->query($sql);
}
/**
* This method gets array
*
* @param resource $link
* @return array
*/
public function getAssoc($link)
{
return $link->fetch_assoc();
}
/**
* This method gets array
*
* @param resource $link
* @return array
*/
public function getRow($link)
{
return $link->fetch_row();
}
/**
* This method returns count elements for query SELECT COUNT()
*
* @param $link
* @param $row
* @return string
*/
public function getCount($link, $row = 0)
{
$arr = $link->fetch_array();
return $arr[$row];
}
/**
* Prepare string for sql query
*
* @param string $str
* @return string
*/
public function toSql($str = '')
{
return $this->connect->real_escape_string($str);
}
/**
* Get number rows
*
* @param $link
* @return mixed
*/
public function numRows($link) {
return $link->num_rows;
}
/**
* Get affected rows
*
* @param $link
* @return mixed
*/
public function affectedRows() {
return $this->connect->affected_rows;
}
}Новые скрипты коннектятся уже как положено, к стандартному файлу CMS, который выглядит так.
dbconfig.php (+/-)
define ("DBHOST", $dbHost);
define ("DBNAME", $dbName);
define ("DBUSER", $dbUser);
define ("DBPASS", $dbPass);
define ("PREFIX", "cms");
define ("USERPREFIX", "cms");
define ("COLLATE", "utf8mb4");
define('SECURE_AUTH_KEY', 'blablabla');
$db = new db;