且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从MySQL数据库的所有产品类别,通过PHP和返回JSON数组

更新时间:2023-11-30 12:29:22

有关PHP端这如何可以得到的类别阵列中,并使JSON,为prepared声明这是你如何完成

  $成功= FALSE;
尝试 {

  $胸径=新PDO(mysql的:主机= $主机,数据库名= $ DBNAME,$用户,$通行证);
$成功= TRUE;
}
赶上(PDOException $ E){
$响应[成功] = 0;
$响应[信息] ='连接失败:。 $ E->的getMessage();
$响应[数据] = NULL;
死亡(json_en code($响应));
}

$ query_categories =SELECT CATEGORY_NAME从分类;

尝试{

$某物= $ sth-> prepare($ query_categories);
$成功= TRUE;

}赶上(PDOException $ E){
$响应[成功] = 0;
$响应[信息] ='prepare失败:。 $ E->的getMessage();
$响应[数据] = NULL;
死亡(json_en code($响应));
}

尝试{
$ sth->执行();
$成功= TRUE;
}赶上(PDOException $ E){
$响应[成功] = 0;
$响应[信息] =执行失败:。 $ E->的getMessage();
$响应[数据] = NULL;
死亡(json_en code($响应));
}

$类别= $ sth->使用fetchall(PDO :: FETCH_COLUMN,0); / *获取从DB *所有分类/

/ *的$ query_categories输出
排列
(
    [0] => CAT1
    [1] => CAT2
    [2] => CAT3
    [3] => CAT4
)
* /
/ * JSON EN code $ query_categories
[CAT1,CAT2,CAT3,CAT4]
* /

/ *检查是否存在类或不* /
如果(空($类)){
        $响应[成功] = 0;
        $响应[信息] =没有发现类别;
        $响应[数据] = NULL;
        死亡(json_en code($响应));
        $连接= NULL;

}

如果($成功){
    $响应[成功] = 1;
    $响应[信息] =凯莉;
    $响应[数据] = $类别;
    死亡(json_en code($响应));
    $连接= NULL;
 / *输出
 {成功:0,消息:凯丽,数据:[CAT1,CAT2,CAT3,CAT4]}
 * /
} 其他 {
    $响应[成功] = 2; / *不要你在哪里设置成功2 * /
    $响应[信息] =出事了;
    $响应[数据] = NULL;
    死亡(json_en code($响应));
    $连接= NULL;
}

} 其他 {
        $响应[成功] = 3; / *不要你在哪里设置成功3 * /
        $响应[信息] =墙上的另一块砖;
        $响应[数据] = NULL;
        死亡(json_en code($响应));
        $连接= NULL;
}
 

我不精通Java,但这里是引用How解析一个JSON,把它的值转换成数组?

  / *店JSON在the_json *串/
JSONObject的myjson =新的JSONObject(the_json);
JSONArray the_json_array = myjson.getJSONArray(数据);
 

  

注意:确保在Java端,你必须从JSON检查成功,这   必须== 1 the_json_array不是null

PHP数据对象

I have an android app. I want to send a get request to the get_categories.php file. In the get_categories.php file I want to

$query_categories        = "SELECT category_name FROM categories";

and return all the categories found in that table into a json array.

How can I do that?

This is my incomplete code:

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;
}

Later on, in my Java code, how do I decode that? Usually up until this point, in my other JSON transfers, I've received normal Json object with no arrays and read them this way:

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

But how do I decode an array?

For php side this how you can get the categories array and make json,for prepared statements this is how you can accomplish

$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;
}

I am not good at java but here is the reference How to parse a JSON and turn its values into an Array?

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

Note: make sure on java side you must check the success from json which must be ==1 and the_json_array is not null

PHP Data Objects