代码之家  ›  专栏  ›  技术社区  ›  Catfish

试图将数组从一个类传递到另一个类

  •  0
  • Catfish  · 技术社区  · 15 年前

    请帮忙。

    <?php
    include('dates.php');
    
    $events = new dates();
    echo $events->getNumberEvents(); <-----returns the number of rows in the table (Works as expected)
    
    $results = $events->getRows(); <------Doesn't work.
    
    ?>
    

    <?php
    /********************************************
    * Class to connect and get dates from a database 
    *********************************************/
    require 'phpDatabaseClass.php';
    
    class dates{
    
        private $db;
        private $arr2 = array();
    
        /****************************
        * Constructor which creates the $db variable
        ****************************/
        function __construct() 
        {
            $this->db = new phpDatabaseClass();
        }   
    
        /****************************
        * Function which calls the database class and returns the number of rows
        ****************************/
        public function getNumberEvents()
        {   
            $sql = "select count(*) from events";           
            return $this->db->queryNumber($sql);
        }
    
        /****************************
        * Function which calls the database class and returns the actual rows
        ****************************/
        public function getRows() 
        {
            $sql = "select * from events";
    
            $this->arr2 = $this->db->queryString($sql);
    
            foreach($this->arr2 as $key=>$val)
            {
                //$this->arr2 = $val;
                //echo $val;    
            }
    
        }
    }
    ?>
    

    phpDatabaseClass类

    <?php
    /********************************************
    * Class to connect and query a mysql database 
    *********************************************/
    require 'config.php';
    
    class phpDatabaseClass {
    
        private $db;
        private $arr = array();
    
        /****************************
        * Constructor function which makes the connection to the database
        ****************************/
        public function __construct()
        {
            try 
            {
                $this->db = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USERNAME, DB_PASSWORD);
            }
            catch(PDOexception $e)
            {
                echo 'Can\'t connect to database. Please contact the site administrator';
            }
        }
    
        /****************************
        * Function which returns the number of rows
        ****************************/
        public function queryNumber($sql)
        {                           
            $stmt = $this->db->prepare($sql);
            $stmt->execute();
            $resultsCount = $stmt->fetchColumn();
    
            return $resultsCount;
        }
    
    
        /****************************
        * Function which returns the data in the rows
        ****************************/
        public function queryString($sql)
        {                           
            $stmt = $this->db->prepare($sql);
    
            $stmt->execute();
    
            $results = $stmt->fetch(PDO::FETCH_ASSOC);
    
            foreach($results as $key=>$val)
            {
                $this->arr = $val;
                //echo $this->arr;
            }
    
            return array($this->arr);
        }
    }
    ?>
    
    1 回复  |  直到 15 年前
        1
  •  2
  •   Gazler    15 年前
    public function getRows() 
    {
        $sql = "select * from events";
    
        $this->arr2 = $this->db->queryString($sql);
        $return = array();
        foreach($this->arr2 as $key=>$val)
        {
            $return[$key] = $val;  
        }
        return $return;
    }
    

    如果你想在那个阶段做任何事的话。要简单地返回,您可以执行以下操作:

    return $this->db->queryString($sql);
    

    要做的事情:

    foreach($results as $result)
    {
    
        echo $result->fieldname;
    
    }
    

    foreach($results as $result)
    {
        foreach($result as $key => $value)
        {
            echo $key.': '.$value;
        }
    
    }
    

    作为对象而不是数组进行访问的原因是mysqli将行作为对象返回。

    推荐文章