Php 未捕获的TypeError:PDO::_construct()要求参数1为字符串
我搜索了论坛,但没有找到任何与我的问题直接相关的东西。我对PDO和类OOP创建相当陌生。我正在尝试创建一个数据库连接类,以便根据需要实例化连接。我在实例化连接时遇到问题 文件组织:Php 未捕获的TypeError:PDO::_construct()要求参数1为字符串,php,pdo,Php,Pdo,我搜索了论坛,但没有找到任何与我的问题直接相关的东西。我对PDO和类OOP创建相当陌生。我正在尝试创建一个数据库连接类,以便根据需要实例化连接。我在实例化连接时遇到问题 文件组织: parent directory (folder) | private (folder) | config.php classes (folder) |
parent directory (folder)
|
private (folder)
|
config.php
classes (folder)
|
class1.class.php
DatabaseConnection.class.php
db_cred.inc.php
public (folder)
|
search.php
<?php
require_once 'db_cred.inc.php';
// creating db connection class
class DatabaseConnect {
private $server = 'DB_HOST';
private $database = 'DB_DATABASE';
private $pass = 'DB_PASS';
private $user = 'DB_USER';
private $opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
protected $con;
public function openConnection() {
**ERROR TAKES PLACE HERE**
try {
$this->con = new PDO([$this->server, $this->database, $this->user, $this->pass]);
return $this->con;
} catch(PDOExeption $e){
echo "ERROR: " . $e->getMessage(), (int)$e->getCode();
}
}
public function closeConnection() {
$this->con = null;
}
}
?>
过程:
- 我已经创建了一个数据库凭证php文件“db_cred.inc.php”
- 我在“DatabaseConnect.class.php”文件中有一个名为“DatabaseConnect”的数据库连接类
- 我按如下方式加载这些文件
- 需要一次“db_cred.inc.php”;在“DatabaseConnect.class.php”文件中
- spl_自动加载_注册我所有的类文件
parent directory (folder)
|
private (folder)
|
config.php
classes (folder)
|
class1.class.php
DatabaseConnection.class.php
db_cred.inc.php
public (folder)
|
search.php
<?php
require_once 'db_cred.inc.php';
// creating db connection class
class DatabaseConnect {
private $server = 'DB_HOST';
private $database = 'DB_DATABASE';
private $pass = 'DB_PASS';
private $user = 'DB_USER';
private $opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
protected $con;
public function openConnection() {
**ERROR TAKES PLACE HERE**
try {
$this->con = new PDO([$this->server, $this->database, $this->user, $this->pass]);
return $this->con;
} catch(PDOExeption $e){
echo "ERROR: " . $e->getMessage(), (int)$e->getCode();
}
}
public function closeConnection() {
$this->con = null;
}
}
?>
function my_autoload($class) {
if(preg_match('/\A\w+\Z/', $class)) {
require ('classes/' . $class . '.class.php');
}
}
spl_autoload_register('my_autoload');
在“db_cred.inc.php”中设置凭证
我的数据库连接类:
parent directory (folder)
|
private (folder)
|
config.php
classes (folder)
|
class1.class.php
DatabaseConnection.class.php
db_cred.inc.php
public (folder)
|
search.php
<?php
require_once 'db_cred.inc.php';
// creating db connection class
class DatabaseConnect {
private $server = 'DB_HOST';
private $database = 'DB_DATABASE';
private $pass = 'DB_PASS';
private $user = 'DB_USER';
private $opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
protected $con;
public function openConnection() {
**ERROR TAKES PLACE HERE**
try {
$this->con = new PDO([$this->server, $this->database, $this->user, $this->pass]);
return $this->con;
} catch(PDOExeption $e){
echo "ERROR: " . $e->getMessage(), (int)$e->getCode();
}
}
public function closeConnection() {
$this->con = null;
}
}
?>
PDO for search.php
<?php
// define an array for db connection.
define("DB", [
"DB_HOST" => "mysql:host=localhost",
"DB_USER" => "user",
"DB_PASS" => "pass",
"DB_DATABASE" => "mytestdb",
"DB_CHAR" => "utf8",
]);
?>
<?php
$dbconn = new DatabaseConnect();
$pdo = $dbconn->openConnection();
$sql = "SELECT * FROM report";
foreach ($pdo->query($sql) as $row) {
echo " PI: ".$row['pi'] . "<br>";
}
?>
我不确定是什么导致了这个错误。我确信这是因为我对类和oop缺乏经验。看起来config.php文件工作正常。该类由请求标识,因为错误发生在PDO _构造方法内部。请帮忙
更新-我的工作解决方案
我希望这可能有助于有人提出类似的问题。我还没有完成这一过程的开发,但这却开启了一扇大门
我的修订课程
<?php
// Associating db_cred.inc.php with class
require_once('db_cred.inc.php');
// Creating db connection class
class DatabaseConnect {
/*
!! Assigning defined constants per define('DB_CONSTANT', 'value') loaded from "db_cred.inc.php" file to variables
!! "db_cred.inc.php" should not be loaded into the "config.php" file
!! BECAUSE THE VALUES OF THE VARIABLES ($variable) ARE CONSTANTS (DB_CONSTANT), DO NOT USE SINGLE OR DOUBLE QUOTES. THE PDO __CONSTRUCT FUNCTION WILL IGNORE THE VALUE OF THE VARIABLE
*/
private $host = DB_HOST;
private $database = DB_DATABASE;
private $pass = DB_PASS;
private $user = DB_USER;
private $char = DB_CHAR;
// Setting attributes and storing in variable $opt
private $opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
// $con variable will store PDO connection and is set to NULL
private $con;
// Create Connection to database
public function openConnection() {
// Setting $con to null
$this->con = NULL;
// If $con is not NULL make it NULL
if ($this->con === NULL){
try {
// Establish DSN
$dsn = "mysql:host={$this->host};dbname={$this->database};charset={$this->char}";
// Complete the PDO connection
$this->con = new PDO($dsn, $this->user, $this->pass,$this->opt);
// Return the connection and store it in $con
return $this->con;
// Catch any exceptions and store in $e
} catch(PDOExeption $e){
// Echo error and Exception message
echo "ERROR: " . $e->getMessage(), (int)$e->getCode();
}
// If the try/catch block fails, echo that no connection was established
} else {
echo "ERROR: No connection can be established";
}
}
// Close connection and set it to NULL
public function closeConnection() {
if($this->con !== NULL){
$this->con = NULL;
}
}
// create CRUD subclass (Create, Read, Update, Delete)
}
?>
我更改了“db_cred.inc.php”,删除了数组。我可能会重新考虑这个想法。
<?php
// defining DB Credential CONSTANTS to be stored in variables and instantiated by connect class
define("DB_HOST", "localhost");
define("DB_USER", "user");
define("DB_PASS", "pass");
define("DB_DATABASE", "mytestdb");
define("DB_CHAR", "utf8");
// define an array for db connection.
/*define("DB", [
"DB_HOST" => "localhost",
"DB_USER" => "user",
"DB_PASS" => "pass",
"DB_DATABASE" => "mytestdb",
"DB_CHAR" => "utf8",
]);*/
?>
错误自行解释:您传递的是一个数组,应该在其中使用字符串 您需要更改此行:
$this->con = new PDO([$this->server, $this->database, $this->user, $this->pass]);
为此,(首先指定DSN):
错误本身解释为:您传递的是一个数组,您应该在其中使用字符串 您需要更改此行:
$this->con = new PDO([$this->server, $this->database, $this->user, $this->pass]);
为此,(首先指定DSN):
您期望的是,您正在向PDO传递一个数组,并且只传递一个数组。阅读,特别是例子。松开方括号。@lawrencerone谢谢你的回答。我删除了括号,错误从“未捕获类型错误:PDO::_构造()预期参数1为字符串”更改为“未捕获类型错误:PDO::_构造()预期参数4为数组”。从我已链接到的文档中。PDO_u_构造期望<代码>公共PDO::_构造(字符串$dsn[,字符串$username[,字符串$passwd[,数组$options]])顺便说一句,这仍然会失败,因为
$server
不是dsn。您希望什么,您正在向PDO传递一个数组,而只传递一个数组。阅读,特别是例子。松开方括号。@lawrencerone谢谢你的回答。我删除了括号,错误从“未捕获类型错误:PDO::_构造()预期参数1为字符串”更改为“未捕获类型错误:PDO::_构造()预期参数4为数组”。从我已链接到的文档中。PDO_u_构造期望<代码>公共PDO::_构造(字符串$dsn[,字符串$username[,字符串$passwd[,数组$options]])顺便说一下,这仍然会失败,因为$server
不是dsn。谢谢。我确实改变了一点。见下文建立DSN$DSN=“mysql:host={$this->host};dbname={$this->database};charset={$this->char}”;//完成PDO连接$this->con=新PDO($dsn,$this->user,$this->pass,$this->opt);如果我的回答对你有帮助,请接受。(通过单击复选标记),因为它可能在将来帮助其他人。我单击了向上箭头。因为我是新来的,它不会显示,但系统会计算它。箭头下方应该有一个复选标记,以接受答案。它也会给你一些名声。谢谢。。。我是新手。哈哈,谢谢你。我确实改变了一点。见下文建立DSN$DSN=“mysql:host={$this->host};dbname={$this->database};charset={$this->char}”;//完成PDO连接$this->con=新PDO($dsn,$this->user,$this->pass,$this->opt);如果我的回答对你有帮助,请接受。(通过单击复选标记),因为它可能在将来帮助其他人。我单击了向上箭头。因为我是新来的,它不会显示,但系统会计算它。箭头下方应该有一个复选标记,以接受答案。它也会给你一些名声。谢谢。。。我是新手。哈哈