简体   繁体   English

我想基于json-php中输出的第一张表行从第二张表获得输出结果

[英]I want to get output result from second table based on first table rows output in json-php

I have knowledge of PHP but I am still learning Json. 我了解PHP,但仍在学习Json。 First of all I want to clear what I am looking for. 首先,我想弄清楚我要寻找的东西。 I have two tables in mysql database, table1( users ) and table2( business ). 我在mysql数据库中有两个表,table1( users )和table2( business )。 Table "users" contains these rows(id, uid, business_name) and table "business" contains these rows(id, uid, category). 表“用户”包含这些行(id,uid,business_name),表“ business”包含这些行(id,uid,category)。

I have following code in PHP page: 我在PHP页面中有以下代码:

if(isset($_GET['catName'])) {

    $cat = $_GET['catName'];
    $stmt = $pdo->prepare("SELECT id, uid, category FROM business WHERE category = ? ");
    $stmt->execute(array($_GET['catName']));
    $arr = $stmt->fetchAll(PDO::FETCH_ASSOC);

}

I am able to get json output on my html page eg 101, 102, 103 and so on. 我可以在html页面上获取json输出,例如101、102、103等。

But I want to get business_name rows like ABC Business, XYZ Business, 123 Business and so on from second table "business" based on the output uid from first table. 但是我想根据第一张表的输出uid从第二张表“ business”中获得诸如ABC Business,XYZ Business,123 Business之类的business_name行。 In brief, I want business_name output instead of uid output from second table. 简而言之,我希望从第二张表输出business_name而不是uid输出。 Please help me. 请帮我。 Thank you so much in advance. 提前非常感谢您。

You have an associative array, with the results from the query. 您具有一个关联数组,其中包含查询结果。 It sounds like you want the business names, but you are not querying for them. 听起来您想要公司名称,但您没有在查询它们。

So the first step would be fix your broken query! 因此,第一步就是修复损坏的查询!

It's difficult to tell what you want from the query, but you're mixing the users table with the business table, so I'm guessing you really want business names based on users. 很难说出您要查询的内容,但是您要混合使用users表和business表,因此我猜您确实希望基于用户的公司名称。

SELECT b.business_name FROM users u JOIN business b ON u.uid = b.uid WHERE category = ?

Then, you have to access your $arr variable correctly to get the business names 然后,您必须正确访问$ arr变量才能获得公司名称

foreach ($arr as $bus_sql_result) {
    echo $bus_sql_result['business_name']."\n";
}

This is not in JSON format, I'm not sure what JSON has to do with what you want, but if you really want it that way, you could try something like 这不是JSON格式,我不确定JSON与您想要的内容有什么关系,但是如果您确实想要这种方式,则可以尝试类似

$business_names = array();
foreach ($arr as $bus_sql_result) {
    $business_names[] = $bus_sql_result['business_name'];
}
echo json_encode($business_names);

Thank you so much Chris and Jormundir. 非常感谢Chris和Jormundir。 Joining the both tables really solved my problem. 加入两个表确实解决了我的问题。 This is what I have done: 这是我所做的:

$stmt = $pdo->prepare("SELECT business.uid, users.business_name FROM business,users WHERE business.uid = users.uid AND business.category= ? "); $ stmt = $ pdo-> prepare(“ SELECT business.uid,users.business_name FROM business,users where business.uid = users.uid AND business.category =?”);

In html page I have put "business_name" array instead of "uid" and I have got result whatever I was looking for. 在html页面中,我放了“ business_name”数组而不是“ uid”,得到的结果我一直在寻找。

Thanks you so much all of you. 非常感谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM