gpt4 book ai didi

php - 修改php oop mysql查询

转载 作者:行者123 更新时间:2023-11-29 09:11:55 25 4
gpt4 key购买 nike

嘿,感谢之前的帮助,我现在有了一个很棒的函数来查询特定的数据行。

    class Posts{

public static function singleQuery($table, $value){

return mysql_fetch_object(
mysql_query("select * from $table where id=$value"), __CLASS__);

}

}

$set = Posts::singleQuery('settings', '1');
echo $post->title;

我希望修改它,以便它查询以下内容:

SELECT * FROM posts ORDER BY id DESC LIMIT 0, 3"

然后在我的 View /索引页面上创建一个“回显循环”或一个 foreach 类型的交易。像这样的东西:

foreach ($a as $b){  
echo "yadda"
}

我希望这是有道理的..

最佳答案

$result = mysql_query("SELECT * FROM posts ORDER BY id DESC LIMIT 0, 3"), __CLASS__);

while($object = mysql_fetch_object($result)) {
// each round of while has the next line in $object
$return[] = $object;
}

return $return;

...

$array = Posts::multipleQuery(...);
foreach($array AS $row) {
echo $row->title;
}

关于php - 修改php oop mysql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5354826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com