class DataBase {
const DB_HOST = 'localhost';
const DB_USER = 'root';
const DB_PASS = 'root';
const DB_NAME = 'example_class_db';
public $con = null;
protected $mysqli;
public function connect() {
$this->con = true;
try {
$this->mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$this->mysqli->query("SET NAMES 'utf8'");
} catch (mysqli_sql_exception $e) {
throw new Exception($e->getMessage());
}
}
public function getRow($query, $params = []) {
try {
$stmt = $this->mysqli->prepare($query);
$stmt->execute($params);
return $stmt->fetch();
} catch (mysqli_sql_exception $e) {
throw new Exception($e->getMessage());
}
}
}
$db = new Database();
$getRow = $db->getRow("SELECT * FROM users WHERE id = ?", ["1"]); // Ошибку выдает на этой строке , без неё все работает отлично
<?php
class DataBase
{
const DB_HOST = 'localhost';
const DB_USER = 'root';
const DB_PASS = 'root';
const DB_NAME = 'example_class_db';
const DB_CHARSET = 'utf8mb4';
public $mysqli;
public function __construct()
{
try
{
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$this->mysqli = new mysqli(self::DB_HOST, self::DB_USER, self::DB_PASS, self::DB_NAME);
$this->set_charset(self::DB_CHARSET);
}
catch(mysqli_sql_exception $e)
{
throw new Exception($e->getMessage());
}
}
public function preparedQuery($query)
{
$stmt = $this->mysqli->prepare($query);
$stmt->bind_param(str_repeat("s", count($params)) , ...$params);
$stmt->execute();
return $stmt;
}
public function getResult($query, $params = [])
{
if (!$params)
{
return $this->mysqli->query($query);
}
return $this->preparedQuery($query, $params)->get_result();
}
public function getRow($query, $params = [])
{
return $this->getResult($query, $params)->fetch_assoc();
}
}