学习 PHP 中的 OOP,尝试重构我的代码。无法再连接到数据库
我以为我理解了类是如何工作的,然后我尝试了这段代码:
class user
{
var $dbcon;
var $dbinfo;
var $con;
var $error;
function dbConnect()
{
$this->dbinfo['server'] = "localhost";
$this->dbinfo['database'] = "foolish_faith";
$this->dbinfo['user'] = "user";
$this->dbinfo['password'] = "password";
$this->con = "mysql:host=".$dbinfo['server']."; dbname=".$dbinfo['database'];
$this->dbcon = new PDO($con, $dbinfo['user'], $dbinfo['password']);
$this->dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->error = $this->dbcon->errorInfo();
if ($error[0] != "")
{
print "Error!";
print_r($error);
}
}
}
现在它只是吐出这个错误:
致命错误:未捕获的异常 “PDOException”,消息“无效” 数据源名称' in E:\PortableApps\xampp\htdocs\dbcon.php:24 堆栈跟踪:#0 E:\PortableApps\xampp\htdocs\dbcon.php(24): PDO->__construct('', NULL, NULL) #1 E:\PortableApps\xampp\htdocs\login.php(4): user->dbConnect() #2 {main} 抛出 E:\PortableApps\xampp\htdocs\dbcon.php 第 24 行
有人能看出我做错了什么吗,因为我确信这与我在课堂上缺乏知识有关?
Thought I understood how classes work, then I tried this code:
class user
{
var $dbcon;
var $dbinfo;
var $con;
var $error;
function dbConnect()
{
$this->dbinfo['server'] = "localhost";
$this->dbinfo['database'] = "foolish_faith";
$this->dbinfo['user'] = "user";
$this->dbinfo['password'] = "password";
$this->con = "mysql:host=".$dbinfo['server']."; dbname=".$dbinfo['database'];
$this->dbcon = new PDO($con, $dbinfo['user'], $dbinfo['password']);
$this->dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->error = $this->dbcon->errorInfo();
if ($error[0] != "")
{
print "Error!";
print_r($error);
}
}
}
Now it just spits out this error:
Fatal error: Uncaught exception
'PDOException' with message 'invalid
data source name' in
E:\PortableApps\xampp\htdocs\dbcon.php:24
Stack trace: #0
E:\PortableApps\xampp\htdocs\dbcon.php(24):
PDO->__construct('', NULL, NULL) #1
E:\PortableApps\xampp\htdocs\login.php(4):
user->dbConnect() #2 {main} thrown in
E:\PortableApps\xampp\htdocs\dbcon.php
on line 24
Can anybody see what I'm doing wrong, as I'm sure it has to do with my lack of knowledge when it comes to classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您访问类实例的变量时,您必须使用 ->;操作员。在这种情况下,您可以使用
$this->dbinfo
而不是$dbinfo
并使用$this->con
而不是>$con
。你在左边做得正确,但在右边却错过了一些。When you acces variables of a class instance you have to use the -> operator. In this case you'd use
$this->dbinfo
instead of just$dbinfo
and$this->con
instead of$con
. You've done it correctly on the left side, but missed some on the right.不要在连接字符串中添加空格。 (
"; dbname="
应该是";dbname="
)。另外,在某些情况下,您需要在某些类实例变量前面添加
$this->
($this->con
、>$this->dbinfo
等等)。Don't put spaces in your connection string. (
"; dbname="
should be";dbname="
).Also, there are a couple of instances where you need to add a
$this->
in front of some class instance variables ($this->con
,$this->dbinfo
et cetera).这样做:
$this->dbcon = new PDO($con, $dbinfo['user'], $dbinfo['password']);
Int this:
$this->dbcon = new PDO($this->con, $dbinfo['user'], $dbinfo['password']);
Make this:
$this->dbcon = new PDO($con, $dbinfo['user'], $dbinfo['password']);
Int this:
$this->dbcon = new PDO($this->con, $dbinfo['user'], $dbinfo['password']);
这是 dsn 语法:https://www.php。 net/manual/en/ref.pdo-mysql.connection.php
我没有看到其中有任何空格,也许你应该删除分号后添加的空格......
Here is the dsn syntax : https://www.php.net/manual/en/ref.pdo-mysql.connection.php
I don't see any space in it, maybe you should remove the one you added after the semicolon...