我寫了使用wamp服務器訪問mysql數據庫中的數據的php代碼。正確的PHP文件
這裏是我的php代碼.........
<?php
//connect to the db
$host="localhost"; // Host name
$user="root"; // Mysql username
$pswd=""; // Mysql password
$db="gpsvts_geotrack"; // Database name
// Table name
$conn = mysqli_connect($host,$user,$pswd,$db);
//mysql_select_db($db, $conn);
//run the query to search for the username and password the match
//$query = "SELECT * FROM "." ".$tbl_name. " "."WHERE uname = '$myusername' AND passwd= '$mypassword' ";
$query = "select user_master.uid,device_locator_tbl.imei,device_locator_tbl.speed,device_locator_tbl.datetime,device_locator_tbl.number,device_master.icon
from device_locator_tbl,device_master,device_registration,user_master where user_master.uid=device_registration.uid
AND device_registration.imei=device_master.imei AND device_registration.imei=device_locator_tbl.imei AND user_master.uid=126";
//$query = "SELECT uid FROM $tbl_name WHERE uname = '$un' AND passwd = '$pw'";
$result = mysqli_query($conn,$query) or die("Unable to verify user because : ");
//this is where the actual verification happens
if($row = mysqli_fetch_array($result))
//echo mysql_result($result,0); // for correct login response
{
$rows[] = $row;
}
// close the database connection
mysqli_close($conn);
// echo the application data in json format
echo json_encode($rows);
?>
當運行這個PHP文件我得到的結果如下方式....
[{」 0 「:」 126" , 「UID」: 「126」, 「1」: 「008873407616」, 「IMEI」: 「008873407616」, 「2」: 「36.4」, 「速度」: 「36.4」, 「3」 :「2013-06-28 21:56:07」,「datetime」:「2013-06-28 21:56:07」,「4」:「008873407616」,「number」:「008873407616」,「 5「:」CAR「,」icon「:」CAR「}]
我沒有使用任何coloum名稱來打印0,1,2,3,4,5 ...在這裏您可以看到0:126,它必須是uid:126。它得到了。但我也有不必要的領域。如何克服這個問題。
Imma在這裏直接拋出它,但也許你想開始使用PDO作爲數據庫抽象層? –