我很開心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));
}
?>
嚴格的期限,我知道這些都非常清楚。沒有像最後一分鐘的東西。 – 2014-09-18 22:48:20
是的,我想我會失去理智!! hahah – gas 2014-09-18 22:49:32
這是一個鏈接到[** PDO準備好的陳述**](http://php.net/pdo.prepared-statements)。 – 2014-09-18 22:50:46