1

我编写了用于使用 wamp 服务器访问 mysql 数据库中的数据的 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","speed":"36.4", "3":"2013-06-28 21:56:07","日期时间":"2013-06-28 21:56:07","4":"008873407616","数字":"008873407616", "5":"CAR","icon":"CAR"}]

我没有使用任何coloum名称来打印0,1,2,3,4,5...这里你可以看到0:126,它必须是uid:126。得到了。但我也得到了不必要的字段。如何克服这个问题。

4

4 回答 4

3

尝试更改获取模式

if($row = mysqli_fetch_assoc($result))
于 2013-07-12T05:50:49.683 回答
1

mysqli_fetch_array也给出了带有自定义索引的结果。

所以你需要使用mysqli_fetch_assoc

while($rows = mysqli_fetch_assoc($result)){
  //your code
}
于 2013-07-12T05:53:28.433 回答
1

mysqli_fetch_array获取包含编号元素和关联元素的 n 数组。你想用mysqli_fetch_assoc

于 2013-07-12T05:54:19.743 回答
1

在您的代码中,您使用了“mysqli_fetch_array”,这就是您获得上述结果的原因,使用“mysqli_fetch_assoc”而不是“mysqli_fetch_array”,您将获得只有列名的结果。

于 2013-07-12T06:02:54.743 回答