dqmfo84644 2013-05-28 22:18
浏览 28

PHP数据库类加载行功能(OOP)

I want to print out all content of the 'forum_question' table. What am i doing wrong? No matter what, i will get the message "Nothing found!", but i am sure the $db->connect DOES work, so it must be something with the query or loadRows functions.

This is my database class:

<?php
class Database {
    private $host;
    private $user;
    private $password;
    private $rows;
    private $result;    
    private $dbName;
    private $connection;    
    private $isReady;

    public function __construct() {         
        $this->result = null;
        $this->isReady = false; 
        }

        /* setters */
    public function setHost($host){ $this->host = $host; }
    public function setUser($user){ $this->user = $user; }
    public function setPassword($password){ $this->password = $password; }
    public function setDbName($dbName){ $this->dbName = $dbName; }

    /* Interface functions */
    public function initiate($host=null,$user=null,$password=null,$dbName=null) {
        if(isset($host,$user,$password,$dbName)==false) {
            die("Please provide require settings.");
        }
        $this->setHost($host);
        $this->setUser($user);
        $this->setPassword($password);
        $this->setDbName($dbName);
        $this->isReady = true;
    }

    public function connect() {
        if($this->isReady==false) {
            die("Not ready to connect, please initiate connection");
        }
        $connection_string = "mysql:host=".$this->host.";dbname=".$this->dbName;
        $this->connection = new PDO($connection_string, $this->user, $this->password);
        $this->query("SET NAMES 'utf8'",$this->connection); // ensure character/language support
    }   

    public function disconnect() {
        $this->connection = null;
        $this->isReady = false;
        $this->setHost = null;
        $this->setUser = null;
        $this->setPassword = null;
        $this->setDbName = null;
    }

    public function query($sql) {
        $this->result = $this->connection->query($sql);
    }   

    public function countRows() {
        return $this->result->rowCount(); }

    public function loadRows() {
        if(!$this->result) die("Nothing found!");
        $this->rows = array();
        foreach ($this->result as $row) {
            $this->rows[] = $row;
        }
        return $this->rows;
    }

} // End of Database class
?>

This is my index file:

<?php

require_once 'class_database.php'; 

$db = new Database(); 
$db->initiate("localhost","USER","PASSWORD","DATABASE"); 
$db->connect();

$db->query("SELECT * FROM 'forum_question'");
$db->loadRows();


?>
  • 写回答

1条回答 默认 最新

  • douzi7711 2013-05-28 23:03
    关注

    Why don't you try with different connection script? For instance:

    <?php
    $mysql_server = "localhost";
    $mysql_user = "USER";
    $mysql_password = "PASSWORD";
    $mysql_db = "DATABASE";
    $mysqli = new mysqli($mysql_server, $mysql_user, $mysql_password, $mysql_db);
    if ($mysqli->connect_errno) {
        printf("Connection failed: %s 
    ", $mysqli->connect_error);
        exit();
    }
    $mysqli->set_charset("utf8");
    

    works fine, all you have to do is:

    <?php
    require_once "connection.php";
    $query = "SELECT * FROM 'forum_question'";
    $mysqli->query($query);
    $mysqli->close();
    

    Assuming that you saved the connection script in file "connection.php".

    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答