2014-09-18 61 views
0

我很開心php和我需要你的help.i想讓以下代碼PDO有一個Android應用程序的JSON輸出我想bulit.I嘗試了很多解決方案,但沒有正確的來了,因爲JSON responce.I的couldn還可以找到很好的例子,tutorials.Also我是新的,因爲我用php這麼說我很害怕嘗試複雜的情況轉換PHP文件到PDO

這裏是PHP代碼

這是我的配置文件

db_config.php

<?php 

define('DB_USER', "root"); // db user 
define('DB_PASSWORD', ""); // db password (mention your db password here) 
define('DB_DATABASE', "androidhive"); // database name 
define('DB_SERVER', "localhost"); // db server 
?> 

這是連接文件

db_connect.php 
<?php 


class DB_CONNECT { 

    // constructor 
    function __construct() { 
     // connecting to database 
     $this->connect(); 
    } 

    // destructor 
    function __destruct() { 
     // closing db connection 
     $this->close(); 
    } 

    /** 
    * Function to connect with database 
    */ 
    function connect() { 
     // import database connection variables 
     require_once __DIR__ . '/db_config.php'; 

     // Connecting to mysql database 
     $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error()); 

     // Selecing database 
     $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error()); 

     // returing connection cursor 
     return $con; 
    } 

    /** 
    * Function to close db connection 
    */ 
    function close() { 
     // closing db connection 
     mysql_close(); 
    } 

} 

?> 

get_all_products.php

<?php 

/* 
* Following code will list all the products 
*/ 

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

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

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

// get all products from products table 
$result = mysql_query("SELECT *FROM products") or die(mysql_error()); 

// check for empty result 
if (mysql_num_rows($result) > 0) { 
    // looping through all results 
    // products node 
    $response["products"] = array(); 

    while ($row = mysql_fetch_array($result)) { 
     // temp user array 
     $product = array(); 
     $product["pid"] = $row["pid"]; 
     $product["name"] = $row["name"]; 
     $product["price"] = $row["price"]; 
     $product["created_at"] = $row["created_at"]; 
     $product["updated_at"] = $row["updated_at"]; 

     // 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); 
} 
?> 

我知道是有經驗的人很容易,但我strungle與it.please幫助,因爲我有一個最後期限陳吉偉和現在沒有時間便宜search.Thank你

**如果我發佈我做了什麼,它會是有益的嗎?我沒有發佈它的空間原因** s

編輯 這是我做了什麼以至於任何想法?

$db = new PDO("mysql:host=$dbhost;dbname=$dbname;", $dbuser, $dbpass); 
$query = "Select * FROM products"; 


//execute query 

try { 

    $stmt = $db->prepare($query); 
    $result = $stmt->execute(HAVE NO IDEA!!!!); 
} 

catch (PDOException $ex) { 
    $response["success"] = 0; 
    $response["message"] = "Database Error!"; 
    die(json_encode($response)); 

} 




$rows = $stmt->fetchAll(); 
if ($rows) { 
    $response["success"] = 1; 
    $response["message"] = "Post Available!"; 
    $response["posts"] = array(); 


    foreach ($rows as $row) { 
     $post    = array(); 
     $post["pid"] = $row["pid"]; 
     $post["name"] = $row["name"]; 
     $post["price"] = $row["price"]; 


     //update our repsonse JSON data 
     array_push($response["posts"], $post); 

    } 



    // echoing JSON response 
    echo json_encode($response); 

} else { 

    $response["success"] = 0; 
    $response["message"] = "No Post Available!"; 
    die(json_encode($response)); 

} 


?> 
+0

嚴格的期限,我知道這些都非常清楚。沒有像最後一分鐘的東西。 – 2014-09-18 22:48:20

+0

是的,我想我會失去理智!! haha​​h – gas 2014-09-18 22:49:32

+0

這是一個鏈接到[** PDO準備好的陳述**](http://php.net/pdo.prepared-statements)。 – 2014-09-18 22:50:46

回答

0

試試這個辦法:

<?php 
$response = array() 
try { 
    //connection 
    $db = new PDO("mysql:host=$dbhost;dbname=$dbname;", $dbuser, $dbpass); 
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    //prepare 
    $query = "Select * FROM products"; 
    $stmt = $db->prepare($query); 
    //get resutlt 
    $result = $stmt->execute(); 
    if ($stmt->rowCount > 0) { 
     $response["success"] = true; 
     $response["message"] = "Post Available!"; 

     //populate post array 
     while($row = $stmt->fetch()){ 
      $response["posts"][] = $row; 
     } 
    }else{ 
     $response["success"] = false; 
     $response["message"] = "No Post Available!";  
    } 
} 

catch (PDOException $ex) { 
    $response["success"] = false; 
    $response["message"] = "Database Error!"; 
} 

echo json_encode($response); 

?> 

Prepared statements and stored procedures


使用獲取所有:

$rows = $stmt->fetchAll(); 
var_dump($rows); 
$response["posts"] = $rows; 
+0

謝謝你,但我有一個警告:array_push()期望參數1是數組,null given.any想法? – gas 2014-09-19 01:19:43

+0

試試'$ response ['posts'] [] = $ row;' – meda 2014-09-19 01:20:59

+0

OK MAN MAN YOU YOU THE BEST !!!!!! IT WORKED !!! THANK YOU !!! Can I ask a little favor?can you向我解釋while循環是如何工作的? – gas 2014-09-19 01:33:47