PHP mySql从表中获取所有项

PHP mySql从表中获取所有项,php,mysql,Php,Mysql,我对PHP和mySql真的很陌生,但我正在尝试找出如何从表中获取所有项。在我的项目处理程序类中 static function getAllItems(){ $items = array(); $db = new Database(); $result = $db->query("SELECT * FROM items"); while($row = $result->fetch_assoc()){

我对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>
当我执行
vardumpvar_dump($allItems[0])时这是返回的,这是正确的

数组(大小=9)'id'=>字符串'1'(长度=1)'title'=>字符串 “测试项目”(长度=9)“说明”=>字符串“测试说明” (长度=16)'imgPath'=>字符串 'images/f141705f42bc438e1fa13ce21a04b67cc4568996-test1.gif' (长度=57)'price'=>字符串'45'(长度=2)'postedTS'=>字符串 '2014-03-19 01:13:34'(长度=19)'updatedTS'=>string'2014-04-16 21:39:19'(长度=19)'published'=>字符串'1'(长度=1)
'category'=>字符串'1'(长度=1)


请尝试修改代码

<?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;
}
//..
}

根据下面给出的
var\u dump
结果:

数组(大小=9)'id'=>string'1'(长度=1)'title'=>string'测试 项目“(长度=9)“说明”=>字符串“测试说明” (长度=16)'imgPath'=>字符串 'images/f141705f42bc438e1fa13ce21a04b67cc4568996-test1.gif' (长度=57)'price'=>字符串'45'(长度=2)'postedTS'=>字符串 '2014-03-19 01:13:34'(长度=19)'updatedTS'=>string'2014-04-16 21:39:19'(长度=19)'published'=>字符串'1'(长度=1)'category' =>字符串“1”(长度=1)

每个
$item
都是一个关联数组,每个项中都有
标题
和其他字段,因此可以使用以下方法访问它:

$item['title'];
$item['description'];

等等。

使用
$item['title']
而不是
$item->getTitle()。
您需要创建所需对象的实例。(项目)

您的代码当前将所有获取的行推送到一个数组中

您需要处理这些行

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;
}
我建议将PDO与PHP()结合使用,因为它可以为您完成所有这些“注入”(如果您想为多个数据库构建应用程序,效果会更好),如下所示:

$yourstatement->fetchObject("Item")

'getTitle()`函数在哪里?@Rorschach这只是我的Item类中的一个基本getter函数。问题是getTitle没有显示在代码中。。。请显示所有相关代码确保它不是一个关联数组,通过
var\u dump($items[0])
@WereWolf TheAlpha I在底部的编辑中包含了var\u dump响应。是的,这就是它,如下所述。不能使用getter。谢谢
$yourstatement->fetchObject("Item")