我正试图从课外访问这些数据。代码在不基于类的情况下工作,但是一旦我将它转换为类就停止了。我希望能够以数组单个索引的形式访问数据。代码如下:
//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();
//