gpt4 book ai didi

php - 连接两个表以显示数据

转载 作者:太空宇宙 更新时间:2023-11-03 12:06:18 25 4
gpt4 key购买 nike

我尝试连接两个表。一个是类别,另一个是子类别。类别表中的id成为子类别表中的cat id

类别

id  name  catimage  
2 cat1 image1

子类别

id  name     subcatimage  catid
1 subcat1 image2 2

代码是

<?php
ob_start();
require_once('config.php');

$selectsubcategory = mysql_query("SELECT category.name, subcategory.name, category.catimage, subcategory.id, subcategory.catid FROM category INNER JOIN subcategory ON category.id=subcategory.catid ");
$posts = array();
if(mysql_num_rows($selectsubcategory))
{
while($post = mysql_fetch_assoc($selectsubcategory))
{
$posts[] = $post;
}
header('Content-type: application/json');
echo stripslashes(json_encode(array('subcategorylist'=>$posts)));
}
else
{
header('Content-type: application/json');
echo stripslashes(json_encode(array('subcategorylist'=>'No subcategory')));
}
?>

我正在获取所有详细信息,但问题是我没有在结果中获取 category.name。谁能帮忙

P.S 我已经使用过 sql,现在稍后会更改它我关心的是功能部分

最佳答案

subcategory.name 会覆盖您的类别名称。您必须像这样为您的子类别名称使用别名:

 $selectsubcategory = mysql_query("SELECT category.name, subcategory.name AS sub_name, category.catimage, subcategory.id, subcategory.catid FROM category INNER JOIN subcategory ON category.id=subcategory.catid ");    

关于php - 连接两个表以显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26439578/

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