小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

PHP數(shù)組由sql行構(gòu)成json_encode

 印度阿三17 2019-06-28

我無法將使用sql查詢生成的php數(shù)組轉(zhuǎn)換為使用json_encode的JSONObject.我使用谷歌凌空來實(shí)現(xiàn)連接.

當(dāng)涉及單行結(jié)果時(shí),我沒有遇到任何問題,但是當(dāng)有超過1行時(shí),我在我的應(yīng)用程序中得到錯(cuò)誤,這意味著我沒有真正收到JSONObject.

這是我的PHP代碼

if (mysql_num_rows($result) > 0) {

$rutina = array();
while($row = mysql_fetch_assoc($result))
{
    $rutina[] = $row;
}}  

我就是這樣回來的

echo json_encode($rutina);

我知道m(xù)ysql已被棄用,我很快就會(huì)遷移到mysqli.

將我的sql行數(shù)組轉(zhuǎn)換為JSONObject的正確方法是什么?

編輯:

這是我等待JSONObject的android代碼:

JsonObjectRequest solicitudRutina = new JsonObjectRequest(
            Request.Method.POST, //metodo de solicitud
            linkrutina, //url, se cambia en las variables
            map,//el objeto JSON que contiene el usuario que intentaremos descargar
            new Response.Listener<JSONObject>() { //el listener de la respuesta
                @Override
                public void onResponse(JSONObject response) { // si existe respuesta aca se cacha,

                    String temp= response.optString("sinexito");//sinexito tiene el mensaje de error de no encontrar el usuario

                    if(temp.equals("")){//si la rutina existe, iniciamos descarga

                        rutinaview.setText(response.toString());
                        //obtenerRutina(response);
                    }
                    else{
                        Context context = getApplicationContext();
                        CharSequence text = "Problema al descargar la rutina, posiblemente no exita una asignada";
                        int duration = Toast.LENGTH_LONG;

                        Toast toast = Toast.makeText(context, text, duration);
                        toast.show();
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            {
                Context context = getApplicationContext();
                CharSequence text = "Error con la base de datos.";
                int duration = Toast.LENGTH_LONG;

                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
            }
        }
    });
    VolleyApplication.getsInstance().getmRequestQueue().add(solicitudRutina);

我正在響應(yīng)錯(cuò)誤的祝酒詞.我假設(shè)它是因?yàn)槲覜]有得到JSONObject?它僅適用于1行.

解決方法:

通常我使用這些就像要成功解析JSON對象那樣需要做一些事情,頁面頭必須有json作為MIME類型,所以任何其他代碼都可以輕松識(shí)別它.

<?php
header('Content-Type:application/json');
//Your Database query here...
$output = mysqli_fetch_all($rutina,MYSQLI_ASSOC);
echo json_encode($output);

它一直適用于我…不需要使用while循環(huán),它將輸出作為數(shù)據(jù)庫查詢找到的行的關(guān)聯(lián)數(shù)組

來源:https://www./content-2-276301.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請遵守用戶 評(píng)論公約

    類似文章 更多