php - 使用 OOP 从 MySQL 获取数据

标签 php mysql oop

我是 OOP PHP 的初学者。 我正在尝试制作一个将连接、查询和获取数据的类 我完成了以下编码

class MySQL {

    private $set_host;
    private $set_username;
    private $set_password;
    private $set_database;

     public function __Construct($set_host, $set_username, $set_password){
        $this->host = $set_host;
        $this->username = $set_username;
        $this->password = $set_password;
        $con= mysql_connect($this->host, $this->username, $this->password);
        if(!$con){ die("Couldn't connect"); }
    }


    public function Database($set_database)
    {   
        $this->database=$set_database;
        mysql_select_db($this->database)or die("cannot select Dataabase");
    }

    public function Fetch($set_table_name){
        $this->table_name=$set_table_name;
        $query=mysql_query("SELECT * FROM ".$this->table_name); 
    $result= mysql_fetch_array($query);
    }
}

$connect = new MySQL('localhost','root','');
$connect->Database('cms');
$connect->Fetch('posts');

我要实现的是这个

$connect = new MySQL('localhost','root','');
$connect->Database('cms');
$connect->Fetch('posts');

我想使用这样的格式获取数据

echo $result[0];

但我不明白实现这件事的逻辑 请帮忙

谢谢!

最佳答案

您的 Fetch 函数仅从数据库中提取一行,并且您没有返回结果...

该方法不是最好的,但为了实现您正在尝试做的事情:

public function Fetch($set_table_name){
    $query=mysql_query("SELECT * FROM ".$set_table_name); 
    $result = array();
    while ($record = mysql_fetch_array($query)) {
         $result[] = $record;
    }
    return $result;
}

这将使每一行成为 $result 的一部分,但您必须像这样访问它:

你可以像这样调用 fetch 函数:

$result = $connect->Fetch('posts');

echo $result[0]['columnName'];  // for row 0;

或者在一个循环中:

for ($x = 0; $x < count($result); $x++) {
   echo $result[$x][0] . "<BR>";  // outputs the first column from every row
}

也就是说,将整个结果集提取到内存中并不是一个好主意(有些人会说这是一个非常糟糕的主意。)

编辑:看起来你的类(class)还有其他问题......我必须运行,但明天会回来检查,如果其他人没有让你直截了当,我会扩展。

Edit2:好的,将对您的类(class)进行一次检查并尝试解释一些事情:

class MySQL {

  //declaring the private variables that you will access through $this->variable;
  private $host;  
  private $username;
  private $password;
  private $database;
  private $conn;  // Adding the connection, more on this later.

  public function __Construct($set_host, $set_username, $set_password){
    $this->host = $set_host;
    $this->username = $set_username;
    $this->password = $set_password;
    // Combining the connection & connection check using 'or'
    // Notice that I removed the semi-colon after the mysql_connect function
    $this->conn = mysql_connect($this->host, $this->username, $this->password)
                  or die("Couldn't connect");
  }

  public function Database($set_database)
  {   
    $this->database=$set_database;
    // Adding the connection to the function allows you to have multiple 
    // different connections at the same time.  Without it, it would use
    // the most recent connection.
    mysql_select_db($this->database, $this->conn) or die("cannot select Dataabase");
  }

  public function Fetch($table_name){
    // Adding the connection to the function and return the result object instead
    return mysql_query("SELECT * FROM ".$table_name, $this->conn);         
  }

}

$connect = new MySQL('localhost','root','');
$connect->Database('cms');
$posts = $connect->Fetch('posts');

if ($posts && mysql_num_rows($posts) > 0) {
     echo "Here is some post data:<BR>";
     while ($record = mysql_fetch_array($posts)) {
         echo $record[0];  // or a quoted string column name instead of numerical index.
     }
} else {
     echo "No posts!";
}

我希望这对您有所帮助...它至少能让您入门。如果您有更多问题,您应该单独提问。

关于php - 使用 OOP 从 MySQL 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6381250/

相关文章:

php - 如何在 PHP 中使用 PDO 获取结果数组?

javascript - this 与 Javascript 原型(prototype)

perl - perl 类中的多个数据成员

php - Knp 菜单包当前项目 Symfony 2

php - 从mysql逆向到mongodb最合适的方法

PHP-MySQL : How to allow the execution of a huge insert query with more than 40, 000 行

python - 当我在 python 中使用 f 字符串时得到不同的输出

php - 获取 MySql 数据并将其存储到 Javascript 数组中

php - 在 PHP 中获取恰好一周前的时间戳?

php - 如何使用php访问浏览器上可见的s3私有(private)图像