我只看到"已连接"在我的屏幕上没有别的。如何在屏幕上显示mysql表格行?
<?php
class db {
private $conn;
private $host;
private $user;
private $password;
private $baseName;
private $port;
function __construct($params=array()) {
$this->conn = false;
$this->host = 'localhost'; //hostname
$this->user = 'fp0313'; //username
$this->password = ''; //password
$this->baseName = 'test'; //name of your database
$this->port = '3306';
$this->connect();
}
function __destruct() {
$this->disconnect();
}
function connect() {
if (!$this->conn) {
$this->conn = mysql_connect($this->host, $this->user, $this->password);
mysql_select_db($this->baseName, $this->conn);
if (!$this->conn) {
$this->status_fatal = true;
echo 'Connection BDD failed';
die();
}
else {
$this->status_fatal = false;
echo 'Connected';
}
}
return $this->conn;
}
function selectData()
{
$User = $bdd->getOne('SELECT stud_id, stud_voornaam, stud_achternaam FROM student'); // 1 line selection, return 1 line
echo $User['stud_id'].'<br>'; // display the id
echo $User['stud_voornaam'].'<br>'; // display the first name
echo $User['stud_achternaam']; // display the last name
}
function disconnect() {
if ($this->conn) {
@pg_close($this->conn);
}
}
}
$bdd = new db(); // create a new object, class db()
?>
答案 0 :(得分:2)
您似乎并不真正了解PHP类的工作原理,因此我将向您介绍OOP PHP的基础知识。一个完整的答案太长,不能作为答案发布,所以我只是简单介绍一下,并链接到更详细的解释:)
属性本质上是与对象实例相关联的变量。还可以存在与类关联的静态属性。属性可以声明为public,private或protected。 Read more...
与属性类似,它们与类或类本身的实例相关联,方法只是属于类的一部分的函数。方法也可以是静态的,也可以声明为public,private或protected。与上述相同的链接适用于此。
PHP有一些魔术方法&#39;。这些只是为特殊功能保留的方法的名称。例如,__construct
是一种魔术方法,在初始化对象时运行。 __clone
也是一种魔术方法,在克隆对象时运行。要定义其中一个,只需定义一个与魔术方法同名的方法,该方法将在特定点运行,例如__construct
;创建对象时,对于__clone
方法,它将在克隆对象时运行。另外,只需注意 - 您不需要定义所有魔术方法,您只需要定义您希望使用的方法。 Read more...
现在您了解OOP PHP背后的基础知识,让我们深入了解您在问题中提供的课程内容!
首先让我们看一下这部分:
private $conn;
private $host;
private $user;
private $password;
private $baseName;
private $port;
这声明了所有属性。 private
表示这些属性只能从这个类中获得,而不是任何扩展类(参见上面关于扩展类的链接)。
现在让我们来看看:
function __construct($params=array()) {
$this->conn = false;
$this->host = 'localhost'; //hostname
$this->user = 'fp0313'; //username
$this->password = ''; //password
$this->baseName = 'test'; //name of your database
$this->port = '3306';
$this->connect();
}
这不带任何参数。你可以传入一个参数,但不会被使用,如果你什么也没有传入,一个空数组将被赋给变量$ params,它只有一个局部范围,因为它是函数的一部分。
如您所知,此函数将在创建对象时执行,因此这会将值分配给某些属性;它将$conn
设置为false,将$host
设置为&#39; localhost&#39;等。然后它运行connect()
函数(您可以跳到该位以查看它的作用)
现在让我们来看看:
function __destruct(){
$这 - &GT;断开();
}
这定义了__destruct
魔术方法,该方法在对象被销毁时运行,通常发生在脚本的末尾。在这种情况下,该函数只运行disconnect()
函数,我们将在以后使用它。
现在这部分:
function connect() {
if (!$this->conn) {
$this->conn = mysql_connect($this->host, $this->user, $this->password);
mysql_select_db($this->baseName, $this->conn);
if (!$this->conn) {
$this->status_fatal = true;
echo 'Connection BDD failed';
die();
}
else {
$this->status_fatal = false;
echo 'Connected';
}
}
return $this->conn;
}
首先检查$conn
是否设置为false(它使用!
(not)运算符来反转它,因此如果$conn
的反函数,则条件的计算结果为true是真的。)。如果是这种情况,则还没有数据库连接,因此它使用mysql_connect()
创建一个,并将连接分配给$conn
属性。然后它选择数据库。然后检查以确保连接成功。如果不是,则$status_fatal
属性设置为true
,&#34;连接BDD失败&#34;回显,并使用die
停止执行,如果连接成功,$status_fatal
属性设置为false
,并且单词“已连接”。是的回应。这就是你目前所看到的。然后该函数返回$conn
属性。
正如有人在您的问题的评论中指出的那样,您可能希望在之后选择数据库,确定连接成功,因为这意味着您不会尝试选择数据库当连接失败时。
现在让我们来看看这一部分:
function selectData()
{
$User = $bdd->getOne('SELECT stud_id, stud_voornaam, stud_achternaam FROM student'); // 1 line selection, return 1 line
echo $User['stud_id'].'<br>'; // display the id
echo $User['stud_voornaam'].'<br>'; // display the first name
echo $User['stud_achternaam']; // display the last name
}
此方法实际上存在错误。由于这是一个函数,所有变量都有局部范围,因此要定位类属性,需要引用$this
。因此,当您使用$bdd
时,它假定在该局部范围内存在这样的变量,因此您需要将其作为参数传递给函数。
假设您现在已修复该错误,并将对象作为参数传入,则运行该对象的方法GetOne
。我无法告诉你这是做什么的,因为我现在知道$dbb
对象包含的内容。很难解释在不知情的情况下会发生什么,所以我想我会在没有那个对象的情况下重写类似的东西。您应该能够弄清楚如何根据您的情况进行调整。以下是使用mysql查询的工作原理(注意:我使用mysqli,这是应该使用的内容,因为mysql_ *函数现在已经过折旧了。):
function SelectData() {
$result = mysqli_query($this->conn, "SELECT stud_id, stud_voornaam, stud_achternaam FROM student LIMIT 1");
$user = mysqli_fetch_assoc($result);
echo $user['stud_id'] . "<br />\r\n";
echo $user['stud_voornaam'] . "<br />\r\n";
echo $user['stud_achternaam'];
}
这引用了以前在mysqli_query()
方法中查询数据库的连接。然后将结果存储在$result
变量中。然后使用mysqli_fetch_assoc()
将该结果集转换为关联数组,然后进行回显。对象中的函数可能与此类似。
现在让我们来看看这一部分:
function disconnect() {
if ($this->conn) {
@pg_close($this->conn);
}
}
这会关闭数据库连接。我相信这是一个错误,但是,因为您之前已经创建了一个mysql数据库连接,但pg_close()
用于postgreSQL。我相信你想要的是mysql_close()
。
使用$bdd = new db()
创建对象时,会调用构造函数,正如我们之前建立的那样,调用connect方法,该方法回显&#34; connected&#34;建立连接时,这样就可以得到回应。但要显示结果,您需要调用使用selectData()
运算符的->
方法,例如此$bdd->selectData()
。
我试着详细解释所有内容,但如果你不了解某些内容,请随时要求澄清。
答案 1 :(得分:0)
你的构造函数调用this->connect
,这就是为什么你得到&#34; connected&#34;信息。但是,你永远不会调用selectData()。
$bdd = new db(); // create a new object, class db()
$bdd->selectData();
您还需要拨打$ bdd-&gt; disconnect();
答案 2 :(得分:0)
您只是使用$bdd = new db()
创建一个新的数据库对象,并且如果您已连接,则代码提到您正在回显Connected
。如果你想做某事,你必须运行$bdd->selectData()
例如。
答案 3 :(得分:0)
您只能获得“已连接”状态。因为你只创建了这个对象。
使用$bdd->selectData();
将返回您想要的数据。
答案 4 :(得分:0)
//添加第二行
$bdd = new db();
$bdd->selectData();