如何通过php从mysql数据库获取所有产品类别并返回json数组

卡洛延·鲁塞夫(Kaloyan Roussev)

我有一个Android应用程序。我想将get请求发送到get_categories.php文件。我要在get_categories.php文件中

$query_categories        = "SELECT category_name FROM categories";

并将该表中找到的所有类别返回到json数组中。

我怎样才能做到这一点?

这是我不完整的代码:

if (!empty($_GET)) {


    $query_categories        = "SELECT category_name FROM categories";
    $success = false;

    try{
        $sth = $connection->prepare($query_categories);
        //$sth->execute(array(':user_id' => $user_id));
        //$user_items_count = $sth->rowCount();  - these are lines from other php file I've used 

        foreach($)//??

        $success = true;
    } catch (PDOException $ex) {

        $response["success"] = 0;
        $response["message"] = $ex;
        die(json_encode($response));
        $connection = null;
    }

    if($success) {
        $response["success"] = 1;
        $response["message"] = "Kylie";

        die(json_encode($response));
        $connection = null;      

    } else {
        $response["success"] = 2;
        $response["message"] = "something went wrong";
        die(json_encode($response));
        $connection = null;
    }

} else {
        $response["success"] = 3;
        $response["message"] = "Another brick in the wall";   
        echo json_encode($response);
        $connection = null;
}

后来,在我的Java代码中,如何解码呢?通常到现在为止,在我的其他JSON传输中,我已经收到了没有数组的普通Json对象,并以这种方式读取它们:

setUserLastSeen(json.getString(TAG_USER_LAST_SEEN)); //for example.

但是,如何解码数组?

哈里德·朱奈德(M Khalid Junaid)

对于php端,这是如何获取Categories数组并生成json的方法,对于准备好的语句,这是可以完成的方法

$success = false;
try {

  $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$success = true;
}
catch(PDOException $e) {
$response["success"] = 0;
$response["message"] = 'Connection failed: ' . $e->getMessage();
$response["data"]=null;
die(json_encode($response));
}

$query_categories = "SELECT category_name FROM categories";

try{

$sth = $sth->prepare($query_categories );
$success = true;

} catch(PDOException $e) {
$response["success"] = 0;
$response["message"] = 'Prepare failed: ' . $e->getMessage();
$response["data"]=null;
die(json_encode($response));
}

try{
$sth->execute();
$success = true;
} catch(PDOException $e) {
$response["success"] = 0;
$response["message"] = 'Execute failed: ' . $e->getMessage();
$response["data"]=null;
die(json_encode($response));
}

$categories = $sth->fetchAll(PDO::FETCH_COLUMN, 0);/* fetches all categories from db */

/* Output of $query_categories
Array
(
    [0] => cat1
    [1] => cat2
    [2] => cat3
    [3] => cat4
)
*/
/* json encode $query_categories 
["cat1","cat2","cat3","cat4"]
*/

/* check if categories exist or not*/
if(empty($categories)){
        $response["success"] = 0;
        $response["message"] = "No categories found";
        $response["data"]=null;
        die(json_encode($response));
        $connection = null; 

}

if($success) {
    $response["success"] = 1;
    $response["message"] = "Kylie";
    $response["data"]=$categories;
    die(json_encode($response));
    $connection = null;      
 /* output
 {"success":0,"message":"Kylie","data":["cat1","cat2","cat3","cat4"]}
 */
} else {
    $response["success"] = 2;/* don't where you are setting success to 2*/
    $response["message"] = "something went wrong";
    $response["data"]=null;
    die(json_encode($response));
    $connection = null;
}

} else {
        $response["success"] = 3;/* don't where you are setting success to 3*/
        $response["message"] = "Another brick in the wall";  
        $response["data"]=null; 
        die(json_encode($response));
        $connection = null;
}

我不擅长Java,但这里有参考资料如何解析JSON并将其值转换为Array?

/* store json string in  the_json */
JSONObject myjson = new JSONObject(the_json);
JSONArray the_json_array = myjson.getJSONArray("data");

注意:确保在Java端,您必须检查json是否成功,该成功必须== 1并且the_json_array不为null

PHP数据对象

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从WordPress获取WooCommerce产品类别

来自分类Dev

在opencart主页上获取产品类别

来自分类Dev

Woocommerce:获取当前产品类别

来自分类Dev

批量产品类别更新(将“ home”类别添加到所有产品)-Prestashop 1.6.0.6

来自分类Dev

magento每页获取产品类别列表

来自分类Dev

Woocommerce如何获得一级产品类别?

来自分类Dev

如何使用无形来迭代联产品中的所有产品类型?

来自分类Dev

产品类别页面上的结构化数据?

来自分类Dev

通过子句获取WooCommerce产品类别的ID

来自分类Dev

如何用缩略图限制返回的产品类别(WooCommerce)

来自分类Dev

通过WooCommerce按类别显示所有产品

来自分类Dev

如何从当前的WooCommerce产品类别中获取所有产品?

来自分类Dev

php产品类别数组

来自分类Dev

如何仅标记产品类别?

来自分类Dev

如何制作产品类别导航

来自分类Dev

无法通过代码设置产品类别

来自分类Dev

在Magento块中获取产品类别ID

来自分类Dev

库存产品类别

来自分类Dev

Magento-获取当前类别中的所有产品

来自分类Dev

MySQL筛选器产品类别AND&OR

来自分类Dev

产品类别页面上的结构化数据?

来自分类Dev

woocommerce获取产品类别ID

来自分类Dev

(1.9)获取所有产品类别的ID(在产品页面上)

来自分类Dev

获取woocommerce类别的所有产品ID

来自分类Dev

通过搜索词预测产品类别

来自分类Dev

获取 WooCommerce 中顶级产品类别的自定义数组

来自分类Dev

如何在插件文件中获取 Woocommerce 产品类别

来自分类Dev

通过类别返回属于某个部门的所有产品的 SQL 过程

来自分类Dev

获取与产品 ID 匹配的产品类别