Json Get 1 record PHP OLEDB

di il
1 risposte

Json Get 1 record PHP OLEDB

Ciao a tutti sto tentando di leggere 1 record dalla mia app "richiedendo un tag nominato "pid" con uno script Php con Driver OLEDB(devo leggerlo su un DB Microsoft ACCESS) per leggerli tutti ho realizzato questo e funziona:
<?php

/* La query SQL,  */
$query="select *from tabripa" ;

/* I parametri di connessione */
$path= "D:/OneDrive/Programmazione/Android/" ;
$db_name= "dati.mdb" ;
$dsource=$path.$db_name ;
$cn_string="Provider=Microsoft.Jet.OLEDB.4.0;" ;
$cn_string.="Data Source=$dsource;" ;
$cn_string.="Jet OLEDB:Database Password=gmpa";


$response = array();

/* La connessione */
if (!file_exists($dsource) ){

die("Il database non esiste") ;

}
$cn= new COM("ADODB.Connection");
$cn->open($cn_string) ;

/* Istanziamo un oggetto Recordset
e inviamo la query attraverso
il metodo Open() */
$rs= new COM("ADODB.Recordset") ;
$rs->Open($query,$cn) ;

/* Ciclo per recuperare i valori dal recordset
EOF= tutto il set di dati è stato esaminato 
e il cursore è giunto in fondo */
if (!$rs->EOF) {
    // looping through all results
    // products node
    $response["products"] = array();
    
    while (!$rs->EOF) {
        // temp user array
        $product = array();
        $product["pid"] = $rs -> Fields["Nbusta"]->value;
        $product["name"] = $rs -> Fields["Nome"]->value;
        $product["price"] = $rs -> Fields["costo"]->value;
        $product["description"] = $rs -> Fields["riparatore"]->value;
       // $product["created_at"] = $row["created_at"];
       // $product["updated_at"] = $row["updated_at"];

	    $rs -> MoveNext();


        // push single product into final response array
        array_push($response["products"], $product);
    }
    // success
    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "No products found";

    // echo no users JSON
    echo json_encode($response);
}

/* Chiusura Recordset (da non farsi nelle query di comando) */
$rs->Close() ;

?>
Ora devo fare l'equivalente di questo script MYSQL però in OLEDB:
<?php

/*
 * Following code will get single product details
 * A product is identified by product id (pid)
 */

// array for JSON response
$response = array();


// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// check for post data
if (isset($_GET["pid"])) {
    $pid = $_GET['pid'];

    // get a product from products table
    $result = mysql_query("SELECT *FROM products WHERE pid = $pid");

    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {

            $result = mysql_fetch_array($result);

            $product = array();
            $product["pid"] = $result["pid"];
            $product["name"] = $result["name"];
            $product["price"] = $result["price"];
            $product["description"] = $result["description"];
           // $product["created_at"] = $result["created_at"];
           // $product["updated_at"] = $result["updated_at"];
            // success
            $response["success"] = 1;

            // user node
            $response["product"] = array();

            array_push($response["product"], $product);

            // echoing JSON response
            echo json_encode($response);
        } else {
            // no product found
            $response["success"] = 0;
            $response["message"] = "No product found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No product found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>
HO provato a fare cosi ma credo sia sbagliato:
<?php

/* La query SQL,  */


/* I parametri di connessione */
$path= "D:/OneDrive/Programmazione/Android/" ;
$db_name= "dati.mdb" ;
$dsource=$path.$db_name ;
$cn_string="Provider=Microsoft.Jet.OLEDB.4.0;" ;
$cn_string.="Data Source=$dsource;" ;
$cn_string.="Jet OLEDB:Database Password=gmpa";


$response = array();

/* La connessione */
if (!file_exists($dsource) ){

die("Il database non esiste") ;

}
$cn= new COM("ADODB.Connection");
$cn->open($cn_string) ;

/* Istanziamo un oggetto Recordset
e inviamo la query attraverso
il metodo Open() */
$rs= new COM("ADODB.Recordset") ;


if (isset($_GET["Nbusta"])) {
    $pid = $_GET['Nbusta'];
    $query="select *FROM tabripa WHERE Nbusta = $pid " ;
    // get a product from products table
    $result = $rs->Open($query,$cn) ;

/* Ciclo per recuperare i valori dal recordset
EOF= tutto il set di dati è stato esaminato 
e il cursore è giunto in fondo */
    if (!empty($result)) {
        // check for empty result

    
        if ($result->EOF) 		{
			
		while (!$result->EOF)
        // temp user array
        $product = array();
        $product["pid"] = $result -> Fields["Nbusta"]->value;
        $product["name"] = $result -> Fields["Nome"]->value;
        $product["price"] = $result -> Fields["costo"]->value;
        $product["description"] = $result -> Fields["riparatore"]->value;
       // $product["created_at"] = $row["created_at"];
       // $product["updated_at"] = $row["updated_at"];

	    $results -> MoveNext();


        // push single product into final response array
        array_push($response["products"], $product);
             // echoing JSON response
            echo json_encode($response);
        
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No product found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
/* Chiusura Recordset (da non farsi nelle query di comando) */
$rs->Close() ;

/* Pulizia dell’oggetto Recordset */
//$rs->Release() ;
//$rs= null ;


}
?>[code]

Vi ringrazio in anticipo non sono un esperto si Php ho solo un po di basi,vado al Lavo ro Buona giornata a tutti.

1 Risposte

  • Re: Json Get 1 record PHP OLEDB

    Scusate,non vedevo errori ma provando la app l'output del LogCat è questo:
    
    
    09-14 07:43:43.584 2396-2396/com.example.androidhive E/JSON Parser: Error parsing data org.json.JSONException: End of input at character 0 of 
    09-14 07:43:43.584 2396-2396/com.example.androidhive D/Single Product Details: {"products":[{"pid":71754,"name":"BONETTO ANTONIETTA","price":58088,"description":"GOLD TIME"},{"pid":72262,"name":"MASSAROTTO AUGUSTO","price":0,"description":"GOLD TIME"},{"pid":74786,"name":"ZANIOLO DANILO","price":174264,"description":"GOLD TIME"},{"pid":56477,"name":"BENETTI ENRICA","price":3873,"description":"ORORA LAB S.R.L"},{"pid":74949,"name":"SARZO MICHELA","price":0,"description":"GOLD TIME"},{"pid":58359,"name":"LUCCHETTA","price":7745,"description":"ORORA LAB S.R.L"},{"pid":76441,"name":"BEVILACQUA MARIA ELENA","price":0,"description":"GOLD TIME"},{"pid":68551,"name":"18042 TOMBOLATO","price":7745,"description":"DE POLI FONTANIVA"},{"pid":69208,"name":"2163","price":25172,"description":"DE POLI S.R.L"},{"pid":70113,"name":"GIOVANNA","price":15490,"description":"SETTE GIOIELLI"},{"pid":79740,"name":"3846","price":1936,"description":"ORORA SRL"},{"pid":100082,"name":"ALLEGRO CRISTINA","price":0,"description":"GOLD TIME"},{"pid":80022,"name":"ROSSI ANGELA","price":5809,"description":"ORORA LAB S.R.L"},{"pid":79980,"name":"2505","price":58088,"description":"ORORA LAB S.R.L"},{"pid":80806,"name":"TESSERO","price":1936,"description":"F.LLI ROMIO"},{"pid":100448,"name":"PIANTELLA CHIARA","price":9681,"description":"GOLD TIME"},{"pid":81464,"name":"16188","price":7745,"description":"ORORA LAB S.R.L"},{"pid":100704,"name":"LIUBA","price":0,"description":"GOLD TIME"}],"success":1}
    09-14 07:43:43.584 2396-2396/com.example.androidhive W/System.err: org.json.JSONException: No value for product
    09-14 07:43:43.584 2396-2396/com.example.androidhive W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
    09-14 07:43:43.584 2396-2396/com.example.androidhive W/System.err:     at org.json.JSONObject.getJSONArray(JSONObject.java:584)
    09-14 07:43:43.584 2396-2396/com.example.androidhive W/System.err:     at com.example.androidhive.EditProductActivity$GetProductDetails$1.run(EditProductActivity.java:142)
    09-14 07:43:43.584 2396-2396/com.example.androidhive W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
    09-14 07:43:43.584 2396-2396/com.example.androidhive W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
    09-14 07:43:43.584 2396-2396/com.example.androidhive W/System.err:     at android.os.Looper.loop(Looper.java:148)
    09-14 07:43:43.584 2396-2396/com.example.androidhive W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
    09-14 07:43:43.584 2396-2396/com.example.androidhive W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
    09-14 07:43:43.584 2396-2396/com.example.androidhive W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    09-14 07:43:43.584 2396-2396/com.example.androidhive W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    09-14 07:43:44.117 2396-2396/com.example.androidhive D/gralloc_ranchu: gralloc_unregister_buffer: exiting HostConnection (is buffer-handling thread)
                                                                           
                                                                           [ 09-14 07:43:44.118  2396: 2396 D/         ]
                                                                           HostConnection::get() New Host Connection established 0xafecb880, tid 2396
    09-14 07:43:44.120 2396-2396/com.example.androidhive D/gralloc_ranchu: gralloc_unregister_buffer: exiting HostConnection (is buffer-handling thread)
                                                                           
                                                                           [ 09-14 07:43:44.125  2396: 2396 D/         ]
                                                                           HostConnection::get() New Host Connection established 0xafecb880, tid 2396
    09-14 07:43:44.137 2396-2396/com.example.androidhive D/gralloc_ranchu: gralloc_unregister_buffer: exiting HostConnection (is buffer-handling thread)
                                                                           
                                                                           [ 09-14 07:43:44.267  2396: 2396 D/         ]
                                                                           HostConnection::get() New Host Connection established 0xafecb880, tid 2396
    

    Presumo non trovi i dati,se provo dal browser lo stesso file ma per Mysql l'output è questo
    {"success":0,"message":"Requiredfield(s) is missing"}
Devi accedere o registrarti per scrivere nel forum
1 risposte