简体   繁体   English

如何使用mysqli对象从SQL查询输出多行

[英]How to output multiple rows from an SQL query using the mysqli object

Assuming that the mysqli object is already instantiatied (and connected) with the global variable $mysql, here is the code I am trying to work with. 假设mysqli对象已经使用全局变量$ mysql实例化(并连接),这是我要使用的代码。

class Listing {
private $mysql;
function getListingInfo($l_id = "", $category = "", $subcategory = "", $username = "", $status = "active") {
        $condition = "`status` = '$status'";
        if (!empty($l_id)) $condition .= "AND `L_ID` = '$l_id'";
        if (!empty($category)) $condition .= "AND `category` = '$category'";
        if (!empty($subcategory)) $condition .= "AND `subcategory` = '$subcategory'";
        if (!empty($username)) $condition .= "AND `username` = '$username'";
        $result = $this->mysql->query("SELECT * FROM listing WHERE $condition") or die('Error fetching values');
        $this->listing = $result->fetch_array() or die('could not create object');
        foreach ($this->listing as $key => $value) :
            $info[$key] = stripslashes(html_entity_decode($value));
        endforeach;
        return $info;
    }
}

there are several hundred listings in the db and when I call $result->fetch_array() it places in an array the first row in the db. 在数据库中有数百个列表,当我调用$ result-> fetch_array()时,它将数组中的第一行放置在数组中。 however when I try to call the object, I can't seem to access more than the first row. 但是,当我尝试调用该对象时,似乎只能访问第一行。 for instance: $listing_row = new Listing; 例如:$ listing_row =新列表; while ($listing = $listing_row->getListingInfo()) { echo $listing[0]; while($ listing = $ listing_row-> getListingInfo()){echo $ listing [0]; } }

this outputs an infinite loop of the same row in the db. 这将输出数据库中同一行的无限循环。 Why does it not advance to the next row? 为什么不前进到下一行? if I move the code: 如果我移动代码:

$this->listing = $result->fetch_array() or die('could not create object');
        foreach ($this->listing as $key => $value) :
            $info[$key] = stripslashes(html_entity_decode($value));
        endforeach;

if I move this outside the class, it works exactly as expected outputting a row at a time while looping through the while statement. 如果将其移出类,它将在按while语句循环时一次输出一行,这与预期的完全一样。 Is there a way to write this so that I can keep the fetch_array() call in the class and still loop through the records? 有没有一种方法可以编写此代码,以便我可以将fetch_array()调用保留在类中,并且仍然可以遍历记录?

Your object is fundamentally flawed - it's re-running the query every time you call the getListingInfo() method. 您的对象从根本上来说是有缺陷的-每次您调用getListingInfo()方法时,它都会重新运行查询。 As well, mysql_fetch_array() does not fetch the entire result set, it only fetches the next row, so your method boils down to: 同样, mysql_fetch_array()不会获取整个结果集,而只会获取下一行,因此您的方法可以归结为:

  1. run query 运行查询
  2. fetch first row 获取第一行
  3. process first row 处理第一行
  4. return first row 返回第一行

Each call to the object creates a new query, a new result set, and therefore will never be able to fetch the 2nd, 3rd, etc... rows. 对对象的每次调用都会创建一个新查询,一个新结果集,因此将永远无法获取第二,第三等行。

Unless your data set is "huge" (ie: bigger than you want to/can set the PHP memory_limit), there's no reason to NOT fetch the entire set and process it all in one go, as shown in Jacob's answer above. 除非您的数据集“巨大”(即:比您想要/可以设置的PHP memory_limit大),否则没有理由不获取整个集合并一次性处理所有数据,如上面的Jacob回答所示。

as a side note, the use of stripslashes makes me wonder if your PHP installation has magic_quotes_gpc enabled. 附带说明,使用反斜杠使我想知道您的PHP安装是否启用了magic_quotes_gpc。 This functionality has been long deprecrated and will be removed from PHP whenever v6.0 comes out. 长期以来,此功能已被弃用,每当v6.0出现时,它将从PHP中删除。 If your code runs as is on such an installation, it may trash legitimate escaping in the data. 如果您的代码按这种安装方式运行,则可能会浪费合法的转义数据。 As well, it's generally a poor idea to store encoded/escaped data in the database. 同样,将编码/转义的数据存储在数据库中通常不是一个好主意。 The DB should contain a "virgin" copy of the data, and you then process it (escape, quote, etc...) as needed at the point you need the processed version. 数据库应包含数据的“原始”副本,然后在需要处理版本时根据需要对其进行处理(转义,引用等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM