使用以下代码从特定ID
获取SQL
的值
include 'classes/connection.class.php';
include 'classes/news.class.php';
$connection = new Connection('localhost', '1video', 'sanoj', '123456');
$news = new News($connection->getDb());
$id=$_GET['name'];
try {
print_r($news->get($id));
它有效 RESULT
Array ( [id] => 103 [title] => bkjbjkuk [location] => 1video.com_81e81be69867c77aea1b9f630c4cf482.mp4 [thumb] => 1video.com_81e81be69867c77aea1b9f630c4cf482.jpg [views] => 1 [likes] => 1 [uploader] => lawrence [added_on] => 19-Feb-16 [tags] => hhuhuhuo [duration] => [comments] => [shortstory] => hihouihohoh )
但我需要在特定位置回显结果但不起作用
方法1
include 'classes/connection.class.php';
include 'classes/news.class.php';
$connection = new Connection('localhost', '1video', 'sanoj', '123456');
$news = new News($connection->getDb());
$id = $_GET['name'];
try {
print_r($news->get($id));
foreach ($news->result() as $row) {
$news= $row->title;
?>
<h3><?php echo $vidid ?></h3>
Fatal error: Call to undefined method News::result()
有人可以指导我如何echo
array()
在不同的位置
课程新闻
class News {
private $db;
private $newsByPage = 5;
public function __construct(PDO $db)
{
$this->db = $db;
}
public function get($id)
{
$q = $this->db->prepare("SELECT * FROM videos WHERE id = :id");
$q->bindValue(":id", $id, PDO::PARAM_INT);
if(!$q->execute())
{
$errors = $q->errorInfo();
throw new Exception("Error while getting a news (".$errors[2].").");
}
else
{
if($res = $q->fetch(PDO::FETCH_ASSOC)){
return $res;
}else{
throw new Exception("No match for id(".$id.").");
}}
$q->closeCursor();
}
}
答案 0 :(得分:0)
应该是这样的:
try {
$myNews = $news->get($_GET['name']);
echo $myNews['title'];
} // -------