类中的PHP方法不从数据库

时间:2017-09-21 03:43:37

标签: php mysql oop

好的,这应该很简单。

我有一个包含3个不同文本框内容的表,我的类中的方法应该将内容插入到文本框中。

示例TextBoxes(TextArea),其中应输入内容。

enter image description here

我的方法

public function LoadBoxes(){
        $db = DB::getInstance();
        $sql = "SELECT * FROM beta_letsgocontent";
        $stmnt = $db->prepare($sql);
        $stmnt->execute();
        $boxes = $stmnt->fetchAll();
            foreach ($boxes as $box) {
                $data[] = array('Low' => $box['boxLow'],
                    'Medium' => $box['BoxMedium'],
                    'High' => $box['BoxHigh']);
            }
            return $data;
        }//function

这是我的表格(下图),因此表格中的数据/内容应插入文本框中。

enter image description here

因此,当我在content.php上进行测试时,我会调用类方法:

require_once('../classes/class.content.php');
$boxes = new Contents();
$boxes->LoadBoxes();
var_dump($boxes); 

我得到以下回复:

enter image description here

问题

可以看到数组键被返回但是数据库中的数据与数组键不匹配或者方法返回 ...我很难过,不知道我做错了什么这里吗?

我出错的任何建议?可能是因为我没有正确连接到数据库吗?

但是,如果是数据库连接错误,我相信我会收到错误

请记住我是一名学生并仍在学习。

更新

连接架构

class Db {
    private static $instance = NULL;

    private function __construct() {}

    private function __clone() {}

    public static function getInstance() {
        if (!isset(self::$instance)) {
            $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
            self::$instance = new PDO('mysql:host=localhost;dbname=beta', 'root', '', $pdo_options);

        }
        return self::$instance;
    }
}

更新2

我刚刚在test.php页面上执行了以下操作,该页面返回了正确的结果。

require('connection.php');

function LoadBoxes(){
    $db = DB::getInstance();
    $sql = "SELECT * FROM beta_letsgocontent";
    $stmnt = $db->prepare($sql);
    $stmnt->execute();
    $boxes = $stmnt->fetchAll();
    foreach ($boxes as $box) {
        $box[] = array('Low' => $box['BoxLow'],
        'Medium' => $box['BoxMedium'],
        'High' => $box['BoxHigh']);
    }
return $box;
}//function

print_r(LoadBoxes());
?>

enter image description here

1 个答案:

答案 0 :(得分:0)

2发布主要问题;

  1. 您将从类中返回$ data,但不会使用返回的数组填充变量。
  2. 你是var转储对象本身。
  3. 这应该有用;

    require_once('../classes/class.content.php');
    $boxes = new Contents();
    $data  = $boxes->LoadBoxes();
    var_dump($data);