Truy Vấn Database Thông Qua PHP Và MYSQL

Một phần của tài liệu nghiên cứu xây dựng phần mềm quản lý khách hàng trên android (Trang 42)

III. Kỹ Thuật Lập Trình Và Truy Vấn Cơ Sở Dữ Liệu Trong Đề Tài

1.Truy Vấn Database Thông Qua PHP Và MYSQL

Để kết nối được dữ liệu trên localhost ta phải xây dựng một class kết nối với localhost như sau:

dbconfig.php dùng để định nghĩa các trường bắt buộc để kết nối được với localhost

<?php

/*

* All database connection variables */

define('DB_USER',"root"); define('DB_PASSWORD',"");

define('DB_DATABASE',"customer_management"); define('DB_SERVER',"localhost");

?>

Dbconnect.php là class để sử dụng chung cho các hành động như read, update, delete, select đến các table trên localhost

<?php

/**

* A class file to connect to database */ class DB_CONNECT { // constructor function __construct() { //connecting to database $this->connect(); } // destructor function __destruct() { // closing db connection

Báo Cáo Thực Tập Tr 42

$this->close(); }

/**

* Function to connect with database */

function connect() {

// import database connection variables

require_once __DIR__ .'/db_config.php'; mysql_query("SET NAMES 'utf8'");

// 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());

//mysql_set_charset('utf8',$db); // returing connection cursor

return $con;

}

/**

* Function to close db connection */ function close() { // closing db connection mysql_close(); } } ?> - JSON:

JSON (JavaScript Object Notation) là định dạng trao đổi dữ liệu độc lập. Giống như XML. JSON chỉ cho phép lưu trữ dữ liệu dưới dạng chữ và số.JSON được hỗ trợ trực tiếp trong Javascript.Cấu trúc dữ liệu của JSON dựa trên các cặp

Key/Value.- Key là một chuỗi.

Value có thể là chuỗi, số, giá trị boolean hoặc object. (adsbygoogle = window.adsbygoogle || []).push({});

Một JSON Object bao gồm các cặp Key/Value và được bắt đầu bởi dấu "{" và kết thúc bởi dấu "}"

Báo Cáo Thực Tập Tr 43

Một mảng các giá trị được nằm trong cặp dấu ngoặc vuông [ ] và cách nhau bởi dấu phẩy ","

Sử dụng JSON

Ví dụ 1: Dùng JSON để mô tả đối tượng với 2 thuộc tính name, email {

"name" : "Duy hung",

"email" : "duyhung.ws@gmail.com"

}

Ví dụ 2: Dùng JSON để mô tả một mảng các đối tượng : [

{

"name" : "Ba Tai",

"email" : "batai@gmail.com"

},

{

"name" : "Tai Nguyen",

"email" : "nguyentai@mail.com"

}

]

Đây là đoạn code read_all_histoy.php sử dụng mysql để truy vần và json_encode để đọc dữ liệu bảng trên localhost về

<?php

/*

* Following code will list all the history */

Báo Cáo Thực Tập Tr 44

// array for JSON response

$response = array();

// include db connect class

require_once __DIR__ .'/db_connect.php';

// connecting to db

$db = new DB_CONNECT();

mysql_query("SET NAMES 'UTF8'"); (adsbygoogle = window.adsbygoogle || []).push({});

$result = mysql_query("SELECT * FROM history ORDER BY created_at DESC") or die(mysql_error());

// check for empty result

if (mysql_num_rows($result) > 0) {

// looping through all results // history node

$response["history"] = array();

while ($row = mysql_fetch_array($result)) {

// temp user array

$history = array();

$history["pid_his"] =$row["pid_his"]; $history["name_his"] = $row["name_his"]; $history["description"] = $row["description"]; $history["created_at"] = $row["created_at"];

// push single product into final response array

array_push($response["history"], $history); }

// success

//mysql_free_result($result);

$response["success"] = 1;

// echoing JSON response

echojson_encode($response); }else {

// no history found

$response["success"] = 0;

$response["message"] = "No history found";

// echo no users JSON

echojson_encode($response); }

?>

Để chạy được file này chúng ta phải để vào thư mục htdocs trong xampp được cài đặt vào ổ đĩa nào đó, ở đây chọn ổ C.

Báo Cáo Thực Tập Tr 45

Hình 27 . Nơi Lưu file php để chạy trên localhost

Để lấy được JSONArray, ta truy nhập vào

link:http://192.168.1.101/www/PHP_Project/customner_management/

Báo Cáo Thực Tập Tr 46

Hình 28 .file php trên localhost

Sau đó chọn file cần đọc, ở đây ta chọn file read_all_histoy.php. ta được chuỗi đọc từ json. (adsbygoogle = window.adsbygoogle || []).push({});

Báo Cáo Thực Tập Tr 47

Một phần của tài liệu nghiên cứu xây dựng phần mềm quản lý khách hàng trên android (Trang 42)