PHP与Mysql面向对象

PHP与Mysql面向对象,php,mysql,oop,Php,Mysql,Oop,我只在屏幕上看到“已连接”,其他什么都看不到。如何在屏幕上显示mysql表行 <?php class db { private $conn; private $host; private $user; private $password; private $baseName; private $port; function __construct($params=array()) { $this->conn =

我只在屏幕上看到“已连接”,其他什么都看不到。如何在屏幕上显示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()
?>

您的构造函数调用
this->connect
,这就是为什么您会收到“connected”消息。但是,您从不调用selectData()


您还需要调用$bdd->disconnect()

您的构造函数调用
this->connect
,这就是为什么您会收到“connected”消息。但是,您从不调用selectData()


您还需要调用$bdd->disconnect()

您正在使用
$bdd=new db()
创建一个新的db对象,并且正如您的代码所提到的,如果您已连接,您正在回显
已连接的
。如果你想做一些事情,你必须运行
$bdd->selectData()

你只是用
$bdd=new db()
创建了一个新的db对象,正如你的代码所提到的,如果你连接了,你会回显
已连接的
。例如,如果要执行某项操作,必须运行
$bdd->selectData()

由于只创建了对象,因此只能获得“已连接”


使用
$bdd->selectData()将返回所需的数据。

由于您只创建了对象,因此您只能获得“已连接”数据


使用
$bdd->selectData()
将返回所需的数据。

您似乎不太了解PHP类是如何工作的,因此我将带您了解OOP PHP的基础知识。完整的答案太长,无法作为答案发布,所以我只做了一个简短的介绍,并链接到更详细的解释:)

性质 属性本质上是与对象实例关联的变量。还可以有与类关联的静态属性。属性可以声明为公共、私有或受保护

方法 类似于属性,它们与类或类本身的实例相关联,方法只是类的一部分的函数。方法也可以是静态的,也可以声明为public、private或protected。与上述相同的链接适用于此

魔术 PHP有一些“神奇的方法”。这些只是为特殊功能保留的方法的名称。例如,
\u构造
是一种神奇的方法,在初始化对象时运行
\u clone
也是一种神奇的方法,它在克隆对象时运行。要定义其中一个方法,只需定义一个与magic方法同名的方法,该方法将在特定点运行,例如对于
\uu构造
;创建对象时,对于
\u clone
方法,将在克隆对象时运行该方法。另外,请注意,您不需要定义所有神奇的方法,只需要定义您希望使用的方法

与OOP PHP重要部分相关的其他链接,这些链接与您的情况不太相关,但仍然需要了解
在上下文中 现在您了解了OOP PHP背后的基础知识,让我们深入了解您在问题中提供的类的作用

首先让我们看看这部分:

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 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);
    }
}
这声明了所有属性。
private
意味着这些属性只能从这个类中使用,而不能从任何扩展类中使用(请参见上面关于扩展类的链接)

现在让我们看看这个:

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 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);
    }
}
这不需要任何参数。您可以传入一个参数,但不会使用该参数,如果不传入任何参数,则将为变量$params分配一个空数组,该变量只有一个局部作用域,因为它是函数的一部分

正如您现在知道的,该函数将在创建对象时执行,因此这将为某些属性赋值;它将
$conn
设置为false,
$host
设置为'localhost',等等。然后,它运行
connect()
函数(您可以跳到该位查看它的作用)

现在让我们看看这个: 函数_udestruct(){ $this->disconnect(); } 这定义了
\uu destruct
魔术方法,该方法在对象被销毁时运行,通常发生在脚本末尾。在本例中,该函数只运行
disconnect()
函数,稍后我们将讨论该函数

现在这部分:

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 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);
    }
}
这首先检查
$conn
是否设置为false(它使用
(not)运算符将其反转,因此如果
$conn
的倒数为true,则条件将计算为true)。如果是这种情况,则还没有数据库连接,因此它使用
mysql\u connect()
创建一个数据库连接,并将连接分配给
$conn
属性。然后选择数据库。然后检查以确保连接成功。如果不是,则将
$status\u fatal
属性设置为
true
,回显“连接BDD失败”,并使用
die
停止执行,如果连接成功,则将
$status\u fatal
属性设置为
false
,并回显单词“Connected”。这就是你现在看到的。然后,该函数返回
$conn
属性

正如有人在您的问题的评论中指出的,您可能希望在确定连接成功后选择数据库,因为这意味着您将不会在连接失败时尝试选择数据库

现在让我们看看这部分:

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 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);
    }
}
这引用了之前在查询数据库的
mysqli\u query()
方法中建立的连接。然后将结果存储在
$result
变量中<然后使用code>mysqli\u fetch\u assoc()
将结果集转换为关联数组,然后对其进行回显。对象中的函数可能会执行类似的操作