[英]How get entry from two table of MySql using PHP to create Json array
我在MySql中有兩個表,一個用於用戶信息,另一個用於用戶發布的新帖子:
new_post表:
id int(11)
title varchar(100)
content text
created_at datetime
url_image varchar(300)
user_id varchar(23)
用戶表:
uid int(11)
unique_id varchar(23)
name varchar(50)
user_profile_image varchar(300)
email varchar(100)
encrypted_password varchar(80)
salt varchar(10)
created_at datatime
通過使用PHP,我想創建Json數組。 在Json數組中,我想返回new_post條目以及存儲在users表中的user_profile_image,我可以通過user_id和unique_id訪問它。
我創建了PHP文件,並且可以很好地返回new_post條目,但是我不知道如何獲取user_profile_image並隨其返回。
這是我的PHP代碼:
<?php
//Create Database connection
error_reporting(E_ALL ^ E_DEPRECATED);
include 'conf.php';
$pageNumber = $_GET['page_number'];
$sql="select * from new_post order by ID DESC limit ".getpage($pageNumber);
$result= mysql_query($sql);
function getpage($p){
$p--;
return ($p*10).",10";
}
//Create an array
$json_response = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$row_array['id'] = $row['id'];
$row_array['title'] = $row['title'];
$row_array['content'] = ($row['content']);
$row_array['created_at'] = $row['created_at'];
$row_array['url_image'] = $row['url_image'];
//push the values in the array
array_push($json_response,$row_array);
}
array_walk_recursive($json_response, function(&$val) {
$val = utf8_encode($val);
});
echo json_encode($json_response, JSON_UNESCAPED_SLASHES);
print "ok";
?>
知道怎么做嗎?
您可以將兩個表聯接到查詢中,例如:
$sql="select * from new_post left join users on new_post.user_id = users.unique_id order by ID DESC limit ".getpage($pageNumber);
現在,您可以使用$row_array['user_profile_image']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.