我怎么解决这个问题?我正在尝试使用准备好的语句从数据库中获取记录。这适用于本地主机,但不适用于在线托管。
数据库.php
<?php
class Db {
private $host = "localhost";
private $user = "guru77";
private $pwd = "123456";
private $dbName = "testingProjects";
protected function connect(){
$dsn = 'mysql:host='.$this->host.';dbname='.$this->dbName;
$pdo = new PDO($dsn,$this->user,$this->pwd);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
return $pdo;
}
}
post_view.php
<?php
class Post_view extends Post{
public function show(){
$results = $this->showAll();
return $results;
}
}
?>
post.php
<?php
class Post extends Db{
protected function showAll(){
$sql = "SELECT * FROM posts";
$stmt = $this->connect()->query($sql);
$results = $stmt->fetchAll();
return $results;
}
}
?>
索引.php
<?php
require_once('db.php');
require_once('post_view.php');
require_once('post.php');
$obj = new Post_view();
$rows = $obj->show();
?>
<html>
<head>
</head>
<body>
<?php foreach($rows as $row){
echo $row['title'];
}?>
</body>
</html>
输出 -
呼啦一阵风
慕工程0101907
呼如林