0

我想获取 mysql 数据库中的数据,并在每种情况下都像在数据库中一样输出它们。但是当我显示它们时,只看单词"Vi tri cua nguoi dung: Array"而不看数据。我使用 JSON 进行测试,我在屏幕上得到的结果是:

"[{" Kinhdo ":" 106.71246126888674 "," Vido ":" 10.78865449101134 "}]" 

你能帮助我吗?

<?php
mysql_connect("xxxx","xxx","xxx");
mysql_select_db("a4602996_lv"); 
$query_insert="select Kinhdo,Vido from VietMap where id = (select max(id) from VietMap)";
$sql = mysql_query($query_insert);
    if(mysql_num_rows($sql)){
        while($row=mysql_fetch_assoc($sql)){
        $json[] = $row;
    }
}
//print(json_encode($json).'<br/>');
print 'Vi tri cua nguoi dung: '.$json['Kinhdo'];
mysql_close();
?>
4

5 回答 5

2

嗨,请查看以下解决方案,希望这会对您有所帮助

// If you are Expecting only one row from the Query Use the following code block

if(mysql_num_rows($sql)){
  while($row=mysql_fetch_assoc($sql)){
    $json = $row; 
  }
}
//print(json_encode($json).'<br/>');
print 'Vi tri cua nguoi dung: '.$json['Kinhdo'];


/**************************************************************/

// If you are Expecting More than one row from the Query Use the following code block

if(mysql_num_rows($sql)){
  while($row=mysql_fetch_assoc($sql)){
    $json = $row; 
  }
}
//print(json_encode($json).'<br/>');
foreach($json as $j){
  print 'Vi tri cua nguoi dung: '.$j['Kinhdo'];
}
于 2012-12-27T10:43:34.460 回答
2

用这个 :

if(mysql_num_rows($sql))
{
  echo "<pre />";
  while($row=mysql_fetch_assoc($sql))
  {
    print_r($row);
    $json[] = $row;
  }
  echo "</pre>";
}

并且您应该为 $json 数组提供偏移量,例如:

$json[0]['Kinhdo'];    //for 1st record
于 2012-12-27T10:44:09.853 回答
0

使用 var_dump() 或 print_r() 或遍历数组

于 2012-12-27T10:30:01.850 回答
0

这意味着您返回的数据是一个数组。改用print_r _

于 2012-12-27T10:30:25.410 回答
0

You var$json是一个多维数组。

$json[] = $row; // assining array to an array index make a array in array

你需要循环扔$json

于 2012-12-27T10:32:31.427 回答