我想知道是否可以向 mysql 发送一个查询但收到两个数组?
$query = 'SELECT a.username,b.* FROM users a, posts b WHERE b.user_id = a.id';
这应该返回一个像
这样的数组array(
'postid' => 1,
'title' => 'this is a title',
'body' => 'this is a body',
'user' => array(
'id' => 1,
'username' => 'name'
)
)
当前查询返回一个与用户“混合”的数组并一起发布。
最佳答案
看看 MVC 模式。
您可以创建类似 Post
的模型具有属性 postid
, title
, body
和 user
作为数组。在您的 SQL 之后,您可以填充模型。然后您可以访问模型中的数据。
示例:
class Post {
private $title;
public function setTitle($title) {
$this->title = $title;
}
public function getTitle() {
return $this->title;
}
}
要通过查询获得理想的结果是不可能的。
关于php - MySQL - 1 个查询但返回两个数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32181802/