通过oops连接数据库
database connection through oops
我正在使用 OOP PHP 和 PDO 开发数据库连接,但我遇到了这个错误:
Warning: Array to string conversion in C:\xampp\htdocs\VICAS\assets\dbfiles\config.php on line 29
Fatal error: Uncaught TypeError: PDO::__construct(): Argument #4 ($options) must be of type ?array, string given in C:\xampp\htdocs\VICAS\assets\dbfiles\config.php:29 Stack trace: #0 C:\xampp\htdocs\VICAS\assets\dbfiles\config.php(29): PDO->__construct('mysql:host=loca...', 'root', '', 'Array') #1 C:\xampp\htdocs\VICAS\pageincludes\image.php(4): vicass->__construct() #2 {main} thrown in C:\xampp\htdocs\VICAS\assets\dbfiles\config.php on line 29
我是 OOP 的新手,非常感谢任何帮助。
<?php
class vicass
{
var $host;
var $username;
var $password;
var $database;
var $connect;
var $home_page;
var $query;
var $data;
var $statement;
var $filedata;
var $options;
function __construct()
{
$this->host = 'localhost';
$this->username = 'root';
$this->password = '';
$this->database = 'vicas';
$this->home_page = 'http://localhost/Vicas/index.php';
$this->options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
PDO::ATTR_EMULATE_PREPARES => false,
);
$this->connect = new PDO("mysql:host=$this->host; dbname=$this->database", "$this->username", "$this->password", "$this->options");
session_start();
}
function execute_query()
{
$this->statement = $this->connect->prepare($this->query);
$this->statement->execute($this->data);
}
function total_row()
{
$this->execute_query();
return $this->statement->rowCount();
}
function redirect($page)
{
header('location:'.$page.'');
exit;
}
}
?>
如果我排除了这些选项,那么它就可以工作,但我需要添加那些
选项...请指导我。
这与“OOP”无关,警告 PHP 在这种情况下完全不言自明。
PDO::__construct期望$options
参数是一个数组,而这里
$this->connect = new PDO("mysql:host=$this->host; dbname=$this->database", "$this->username", "$this->password", "$this->options");
你将它用双引号括起来,将它变成一个字符串 - 因此出现错误(因为 PHP 不会自动知道如何以字符串格式表示数组)。
删除那些双引号以将数组传递给 __construct
。
N.B。用户名和密码变量周围的引号是多余的,因为这些变量已经包含字符串(对于那些参数,需要一个字符串)。
$this->connect = new PDO("mysql:host=$this->host; dbname=$this->database", $this->username, $this->password, $this->options);
我正在使用 OOP PHP 和 PDO 开发数据库连接,但我遇到了这个错误:
Warning: Array to string conversion in C:\xampp\htdocs\VICAS\assets\dbfiles\config.php on line 29 Fatal error: Uncaught TypeError: PDO::__construct(): Argument #4 ($options) must be of type ?array, string given in C:\xampp\htdocs\VICAS\assets\dbfiles\config.php:29 Stack trace: #0 C:\xampp\htdocs\VICAS\assets\dbfiles\config.php(29): PDO->__construct('mysql:host=loca...', 'root', '', 'Array') #1 C:\xampp\htdocs\VICAS\pageincludes\image.php(4): vicass->__construct() #2 {main} thrown in C:\xampp\htdocs\VICAS\assets\dbfiles\config.php on line 29
我是 OOP 的新手,非常感谢任何帮助。
<?php
class vicass
{
var $host;
var $username;
var $password;
var $database;
var $connect;
var $home_page;
var $query;
var $data;
var $statement;
var $filedata;
var $options;
function __construct()
{
$this->host = 'localhost';
$this->username = 'root';
$this->password = '';
$this->database = 'vicas';
$this->home_page = 'http://localhost/Vicas/index.php';
$this->options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
PDO::ATTR_EMULATE_PREPARES => false,
);
$this->connect = new PDO("mysql:host=$this->host; dbname=$this->database", "$this->username", "$this->password", "$this->options");
session_start();
}
function execute_query()
{
$this->statement = $this->connect->prepare($this->query);
$this->statement->execute($this->data);
}
function total_row()
{
$this->execute_query();
return $this->statement->rowCount();
}
function redirect($page)
{
header('location:'.$page.'');
exit;
}
}
?>
如果我排除了这些选项,那么它就可以工作,但我需要添加那些 选项...请指导我。
这与“OOP”无关,警告 PHP 在这种情况下完全不言自明。
PDO::__construct期望$options
参数是一个数组,而这里
$this->connect = new PDO("mysql:host=$this->host; dbname=$this->database", "$this->username", "$this->password", "$this->options");
你将它用双引号括起来,将它变成一个字符串 - 因此出现错误(因为 PHP 不会自动知道如何以字符串格式表示数组)。
删除那些双引号以将数组传递给 __construct
。
N.B。用户名和密码变量周围的引号是多余的,因为这些变量已经包含字符串(对于那些参数,需要一个字符串)。
$this->connect = new PDO("mysql:host=$this->host; dbname=$this->database", $this->username, $this->password, $this->options);