MySQL
简单是实现android与php mysql数据库的连接。
这里提供的代码只是为了使你能简单的连接 Android 项目和 PHP,MySQL。你不能把它作为一个标准或者安全编程实践。在生产环境中,理想情况下你需要避免使用任何可能造成潜在注入漏洞的代码(比如 MYSQL 注入)。
涉及的PHP文件
db_connect.php
connect();
}
// destructor
function__destruct() {
// closing db connection
$this->close();
}
/**
* Function to connect with database
*/
functionconnect() {
// import database connection variables
require_once__DIR__ . '/db_config.php';
// Connecting to mysql database
$con= mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) ordie(mysql_error());
// Selecing database
$db= mysql_select_db(DB_DATABASE) ordie(mysql_error()) ordie(mysql_error());
// returing connection cursor
return$con;
}
/**
* Function to close db connection
*/
functionclose() {
// closing db connection
mysql_close();
}
}
?>
怎么调用 :当你想连接 MySQl 数据库或者执行某些操作时,可以这样使用 db_connect.php
$db= newDB_CONNECT(); // creating class object(will open database connection)
create_product.php
对于上面的代码,JSON 的返回值会是:
当 POST 参数丢失
{
"success": 0,
"message": "Required field(s) is missing"
}
当 product 成功创建
{
"success": 1,
"message": "Product successfully created."
}
当插入数据时出现错误
{
"success": 0,
"message": "Oops! An error occurred."
}
get_product_details.php
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
echojson_encode($response);
} else{
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echojson_encode($response);
}
} else{
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echojson_encode($response);
}
} else{
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echojson_encode($response);
}
?>
The json response for the above file will be
When successfully getting product details
{
"success": 1,
"product": [
{
"pid": "1",
"name": "iPHone 4S",
"price": "300.00",
"description": "iPhone 4S white",
"created_at": "2012-04-29 01:41:42",
"updated_at": "0000-00-00 00:00:00"
}
]
}
When no product found with matched pid
{
"success": 0,
"message": "No product found"
}
get_all_products.php
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
echojson_encode($response);
} else{
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echojson_encode($response);
}
?>
And the JSON response for above code
Listing all Products
{
"products": [
{
"pid": "1",
"name": "iPhone 4S",
"price": "300.00",
"created_at": "2012-04-29 02:04:02",
"updated_at": "0000-00-00 00:00:00"
},
{
"pid": "2",
"name": "Macbook Pro",
"price": "600.00",
"created_at": "2012-04-29 02:04:51",
"updated_at": "0000-00-00 00:00:00"
},
{
"pid": "3",
"name": "Macbook Air",
"price": "800.00",
"created_at": "2012-04-29 02:05:57",
"updated_at": "0000-00-00 00:00:00"
},
{
"pid": "4",
"name": "OS X Lion",
"price": "100.00",
"created_at": "2012-04-29 02:07:14",
"updated_at": "0000-00-00 00:00:00"
}
],
"success": 1
}
When products not found
{
"success": 0,
"message": "No products found"
}
update_product.php
The json reponse of above code, when product is updated successfully
{
"success": 1,
"message": "Product successfully updated."
}
delete_product.php
0) {
// successfully updated
$response["success"] = 1;
$response["message"] = "Product successfully deleted";
// echoing JSON response
echojson_encode($response);
} else{
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echojson_encode($response);
}
} else{
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echojson_encode($response);
}
?>
When product successfully deleted
{
"success": 1,
"message": "Product successfully deleted"
}
When product not found
{
"success": 0,
"message": "No product found"
}