[英]PHP mySql fetch all items from table
我是PHP和mySql的新手,但我正在試圖弄清楚如何從表中獲取所有項目。 在我的項目處理程序類中,我正在做
static function getAllItems(){
$items = array();
$db = new Database();
$result = $db->query("SELECT * FROM items");
while($row = $result->fetch_assoc()){
$items[] = $row;
}
return $items;
}
然后在客戶端我這樣做是為了遍歷返回的項目並顯示它們,但不斷收到錯誤
“致命錯誤:在非對象上調用成員函數getTitle()”
我究竟做錯了什么?
<div class="table-responsive">
<?php
$allItems = ItemHandler::getAllItems();
if(empty($allItems)){
echo '<p class="lead text-center">No listings in this category yet.</p>';
} else {
echo '<table class="table">
<tr>
<th>Image</th>
<th>Item Name</th>
<th>Quantity</th>
<th>Description</th>
<th>Seller</th>
<th>Price</th>
<th>Purchase</th>
</tr>';
foreach($allItems as $item){
echo "<tr>
<td>blank</td>
<td>{$item->getTitle()}</td>
<td>1</td>
<td>{$item->getDescription()}</td>
<td>test seller</td>
<td>{$item->getPrice()}</td>
<td><a href='#'><button type='button' class='btn btn-primary'>Buy</button></a></td>
</tr>";
}
echo '</table>';
}
?>
</div>
我不相信我的Item類中的任何函數都存在問題。 我相信這是我用get all items創建項目的方式。 無論如何,這是代碼。 項目包含在我的客戶端文件中。
class Item {
private $id;
private $title;
private $description;
private $imgPath;
private $category;
private $keywords;
private $postedTimestamp;
private $updatedTimestamp;
private $price;
private $published;
function __construct($id, $title, $description, $imgPath, $category, $keywords, $postedTS, $updatedTS, $price, $published){
$this->setId($id);
$this->setTitle($title);
$this->setDescription($description);
$this->setImgPath($imgPath);
$this->setCategory($category);
$this->setKeywords($keywords);
$this->setPostedTimestamp($postedTS);
$this->setUpdatedTimestamp($updatedTS);
$this->setPrice($price);
$this->setPublished($published);
}
public function getTitle()
{
return $this->title;
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
//other getters/setters of same format
當我做vardumpvar_dump($allItems[0]);
這是返回的,這是正確的......
array(size = 9)'id'=> string'1'(length = 1)'title'=> string'Test item'(length = 9)'description'=> string'test description'(length = 16) 'imgPath'=> string'images'f141705f42bc438e1fa13ce21a04b67cc4568996-test1.gif'(length = 57)'price'=> string'45'(length = 2)'postedTS'=> string'2014-03-19 01:13: 34'(長度= 19)'updatedTS'=>字符串'2014-04-16 21:39:19'(長度= 19)'已發布'=>字符串'1'(長度= 1)
'category'=>字符串'1'(長度= 1)
根據你的var_dump
結果給出如下:
array(size = 9)'id'=> string'1'(length = 1)'title'=> string'Test item'(length = 9)'description'=> string'test description'(length = 16) 'imgPath'=> string'images'f141705f42bc438e1fa13ce21a04b67cc4568996-test1.gif'(length = 57)'price'=> string'45'(length = 2)'postedTS'=> string'2014-03-19 01:13: 34'(長度= 19)'updatedTS'=>字符串'2014-04-16 21:39:19'(長度= 19)'已發布'=>字符串'1'(長度= 1)'類別'=>字符串'1'(長度= 1)
每個$item
都是一個關聯數組,每個項目中都有title
和其他字段,因此可以使用以下方法訪問它:
$item['title'];
$item['description'];
等等。
使用$item['title']
而不是$item->getTitle().
請嘗試修改代碼。
<?php
$con = mysql_connect("localhost", "root", "mypass") or
die("Could not connect: " . mysql_error());
mysql_select_db("tutorials");
$result = mysql_query("select * from tutorials");
echo "<table border='1' style='border-collapse: collapse'>";
echo "<th>Name of theTutorial</th><th>Pages</th><th>Examples</th><th>Author</th>";
while ($row = mysql_fetch_array($result))
{
echo"<tr><td>".$row['name']."</td><td>".$row['no_of_pages']."</td><td>".$row['no_of_examples']."</td><td>".$row['author']."</td></tr>";<br>}<br>echo "</table>";<br>mysql_close($con);
?>
請根據需要更改您的代碼。 我希望你能100%解決
嘗試創建一個包含表模型的類,然后初始化setter和getter,之后你可以使用fnctions即
class model{
private $title;
private $description;
private $price;
//...
public function getTitle(){
return $title;
}
//..
}
您需要創建所需對象的實例。 (項目)
您的代碼當前將所有獲取的行推送到數組中。
您需要處理這些行。
static function getAllItems(){
$items = array();
$db = new Database();
$result = $db->query("SELECT * FROM items");
while($row = $result->fetch_assoc()){
$items[] = new Item($row["id"], $row["title"], $row["description"], $row["imgPath"], $row["category"], $row["keywords"], $row["postedTS"], $row["updatedTS"], $row["price"], $row["published"]);
}
return $items;
}
我建議在PHP中使用PDO( http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers ),因為它可以為你做所有這些'注入'(如果你想為多個數據庫構建應用程序,那就更好) :
$yourstatement->fetchObject("Item")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.