php - 使用函数来简化 CMS 主题

标签 php mysql function

我正在制作自己的 CMS。我有一些代码可以选择要在页面上回显的数据库行,如下所示:

$query = mysql_query("SELECT * FROM posts order by id desc") or die(mysql_error()); 

但是,每当我尝试将其放入函数中并调用该函数而不是使用那么长的代码行时,什么也没有发生。我是否缺少 PHP 函数的某些内容?

function posts() {
     mysql_query("SELECT * FROM posts order by id desc") or die(mysql_error()); `
}
while($row = mysql_fetch_array(posts())) {
   $id = $row['id'];
   echo $id;
}

最佳答案

您需要返回结果:

function posts() {
     return mysql_query("SELECT * FROM posts order by id desc") or die(mysql_error()); `
}

关于php - 使用函数来简化 CMS 主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6039505/

相关文章:

php - 如何使用foreach循环进入另一个foreach循环来遍历c​​odeigniter中的两个不同数据表?

php - 无法吐出 mysqli 随机数

MYSQL:想要将日期列设为NULL

c++ - 关于在 C++ 中返回容器 : pointer VS non-pointer

T-SQL 日期显示

PHP/MySQL 农历

php - 如何在 PHP 中使用 MySQL 事务?

返回 char 的 C 函数给出冲突类型错误

php - 某些设备未收到 Apple 推送通知

php - MySQL - 从表中选择全部并从另一个表中选择相关行