我正在使用此方法从db
获取数据class Database {
private static $_instance = null;
private $_pdo,
$_query,
$_error = false,
$_results,
$_count = 0;
private function __construct() {
try {
$this->_pdo = new PDO('mysql:host='.Config::get('mysql/host').';dbname='.Config::get('mysql/db'),Config::get('mysql/username'),Config::get('mysql/password'));
} catch (PDOException $e) {
die($e->getMessage());
}
}
public static function getInstance() {
if (!isset(self::$_instance)) {
self::$_instance = new Database();
}
return self::$_instance;
}
public function query($sql, $params = array()) {
$this->_error = false;
if ($this->_query = $this->_pdo->prepare($sql)) {
$x = 1;
if (count($params)) {
foreach ($params as $param) {
$this->_query->bindValue($x, $param);
$x++;
}
}
if ($this->_query->execute()) {
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
} else {
$this->_error = true;
}
}
return $this;
}
public function action($action, $table, $where = array()) {
if (count($where) === 3) {
$operators = array('=','>','<','>=','<=','<>');
$field = $where[0];
$operator = $where[1];
$value = $where[2];
if (in_array($operator, $operators)) {
$sql = "{$action} FROM {$table} WHERE ${field} {$operator} ?";
if (!$this->query($sql, array($value))->error()) {
return $this;
}
}
}
return false;
}
public function get($table, $where) {
return $this->action('SELECT *', $table, $where);
}
public function delete($table, $where) {
return $this->action('DELETE', $table, $where);
}
public function insert($table, $fields = array()) {
if (count($fields)) {
$keys = array_keys($fields);
$values = null;
$x = 1;
foreach ($fields as $field) {
$values .= '?';
if ($x<count($fields)) {
$values .= ', ';
}
$x++;
}
$sql = "INSERT INTO {$table} (`".implode('`,`', $keys)."`) VALUES({$values})";
if (!$this->query($sql, $fields)->error()) {
return true;
}
}
return false;
}
public function update($table, $id, $fields = array()) {
$set = '';
$x = 1;
foreach ($fields as $name => $value) {
$set .= "{$name} = ?";
if ($x<count($fields)) {
$set .= ', ';
}
$x++;
}
$sql = "UPDATE {$table} SET {$set} WHERE id = {$id}";
if (!$this->query($sql, $fields)->error()) {
return true;
}
return false;
}
public function results() {
return $this->_results;
}
public function first() {
return $this->_results[0];
}
public function error() {
return $this->_error;
}
public function count() {
return $this->_count;
}
}
this is what i want to get AND this is my table
这是我的代码......
public function getModule() {
$epreuve = $this->_db->get('epreuve', array('concour_code', '=', $this->data()->concour_code));
foreach($epreuve->results() as $epreuve){
echo "<tr><td>".$epreuve->designation."</td>"
.$module = $this->_db->get('module', array('epreuve_code', '=', $epreuve->code ));
foreach($module->results() as $module){
echo "<tr><td>".$epreuve->designation."</td>";
}
"</tr>";
}
}
但我有这个错误
''可捕获的致命错误:类数据库的对象无法转换为字符串''
答案 0 :(得分:0)
错误似乎在这里:
echo "<tr><td>".$epreuve->designation."</td>"
.$module = $this->_db->get('module', array('epreuve_code', '=',
请注意,您没有使用半冒号关闭echo
,并且在$module
之前有一个点,因此PHP尝试使用$ module类对连接回显字符串进行字符串加上迭代也在连接中。你不能这样做。
执行以下操作:
public function getModule() {
$epreuve = $this->_db->get('epreuve', array('concour_code', '=', $this->data()->concour_code));
foreach($epreuve->results() as $epreuve){
echo "<tr>";
echo "<td>".$epreuve->designation."</td>";
$module = $this->_db->get('module', array('epreuve_code', '=', $epreuve->code ));
foreach($module->results() as $module){
echo "<td>".$epreuve->designation."</td>";
}
echo "</tr>";
}
}
Suggesiton:
在你的代码上
foreach($epreuve->results() as $epreuve){
和
foreach($module->results() as $module){
您不应该使用与迭代相同的变量名称。尝试将其更改为
public function getModule() {
$epreuve = $this->_db->get('epreuve', array('concour_code', '=', $this->data()->concour_code));
foreach($epreuve->results() as $epreu){
echo "<tr>";
echo "<td>".$epreu->designation."</td>";
$module = $this->_db->get('module', array('epreuve_code', '=', $epreu->code ));
foreach($module->results() as $mod){
echo "<td>".$epreu->designation."</td>";
}
echo "</tr>";
}
}
注意:HTML表格有点乱,我最好去理解它。根据您的需要改变它。