dshakcsq64956 2016-03-14 00:19
浏览 14

如何将mysql数据从类返回到该类之外?

I am trying to access this data from outside of a class. The code works when it is not class based, but once I convert it to classes it stops. I want to be able to access the data as array single indexes. The code is below:

//Database connection that is working
class Database {
    private $_connection;
    private static $_instance; //The single instance
    private $_host = "localhost";
    private $_username = "root";
    private $_password = "testuser";
    private $_database = "testpassword";
    /*
    Get an instance of the Database
    @return Instance
    */
    public static function getInstance() {
        if(!self::$_instance) { // If no instance then make one
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    // Constructor
    private function __construct() {
        $this->_connection = new mysqli($this->_host, $this->_username,
            $this->_password, $this->_database);

        // Error handling
        if(mysqli_connect_error()) {
            trigger_error("Failed to conencto to MySQL: " . mysqli_connect_error(),
                E_USER_ERROR);
        }
    }
    public function result()
    {
        echo $this->query;

    }
    // Magic method clone is empty to prevent duplication of connection
    private function __clone() { }
    // Get mysqli connection
    public function getConnection() {
        return $this->_connection;
    }
}
//select statement that is not working as a class
class getText
{
    private static $_instance;
    private $field;
    private $result;
    public static function getInstance() {
        if(!self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    public function __construct()
    {
        $db = Database::getInstance();
        $mysqli = $db->getConnection();
        $sql = "SELECT * FROM template_text_boxes WHERE idtemplate_text_boxes='61'";
        $this->result = mysqli_query($mysqli, $sql);
        if ($this>result) {
            while ($row = mysqli_fetch_assoc($this->result)) {
                $this->field[] = array('text1' => $row['text1'], 'text2' => $row['text2'], 'text3' => $row['text3'], 'text4' => $row['text4'], 'text5' => $row['text5'], 'text6' => $row['text6'], 'text7' => $row['text7'], 'text8' => $row['text8'], 'text9' => $row['text9'], 'text10' => $row['text10'], 'text11' => $row['text11'], 'text12' => $row['text12'], 'text13' => $row['text13'], 'text14' => $row['text14'], 'text15' => $row['text15'], 'text16' => $row['text16'], 'text17' => $row['text17'], 'text18' => $row['text18'], 'text19' => $row['text19'], 'text20' => $row['text20'], 'text21' => $row['text21'], 'text22' => $row['text22'], 'text23' => $row['text23'], 'text24' => $row['text24'], 'text25' => $row['text25'], 'text26' => $row['text26'], 'text27' => $row['text27'], 'text28' => $row['text28'], 'text29' => $row['text29'], 'text30' => $row['text30'], 'text31' => $row['text31'], 'text32' => $row['text32'], 'text33' => $row['text33'], 'text34' => $row['text34'], 'text35' => $row['text35'], 'text36' => $row['text36'], 'text37' => $row['text37'], 'text38' => $row['text38'], 'text39' => $row['text39'], 'text40' => $row['text40'], 'text41' => $row['text41'], 'text42' => $row['text42'], 'text43' => $row['text43'], 'text44' => $row['text44'], 'text45' => $row['text45'], 'text46' => $row['text46'], 'text47' => $row['text47'], 'text48' => $row['text48'], 'text49' => $row['text49'], 'text50' => $row['text50'], 'text57' => $row['text57'], 'text58' => $row['text58']);
            }
        }
    }
public static function get_text($this->field, $x)
{
    echo ->field[0][$x];
}

public static function text($field, $x)
{
    return -> field[0][$x];
}
}
//how I want to access each database column
$getText ->text($field,'text49');
$getText = new getText();
//
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制