问:我已经为sqlite编写了代码,以选择特定的列并显示行..
id条件lat long
1 好 0.0 0.0
2 差 0.0 0.0
3 好 0.0 0.0
当我选择“好”时,它应显示我的经度和纬度ID 1,3 ...等等
公共游标getSelectedLocations (字符串搜索){
db = SQLiteHandler 。getWritableDatabase ();
字符串[] selecteditems = { SQLiteHandler 。KEY _ TLATITUDE,SQLiteHandler.KEY_ TLONGITUDE,SQLiteHandler.KEY_ TreeID};
字符串,其中= SQLiteHandler 。KEY _ TCONDITION +“ =?”;
字符串[] whereArgs = {搜索} ;
光标cursor = db 。查询( SQLiteHandler 。 TABLE _ TREE,selecteditems,其中,whereArgs,NULL,NULL,NULL,NULL);
返回游标;
}
现在我想用Mysqli PHP编写:json的示例代码。我想根据上述概念更改此代码
<?php
定义('&#95;&#95; ROOT&#95;&#95;' , 目录名(目录名(&#95;&#95; FILE&#95;&#95;)));
要求&#95; once(&#95;&#95; ROOT&#95;&#95;。'/ public&#95; html / Config.php');
//连接到mysql数据库
$ mysqli = 新的 mysqli ( DB &#95; HOST,DB&#95; USER,DB&#95; PASSWORD,DB&#95; DATABASE);
// json响应数组
$ response = array (“错误” =>假);
//获取Google Map Marker的树详细信息
if ($ stmt = $ mysqli- >查询(“ SELECT * FROM tree” )){
if ($ stmt- > num &#95; rows){
而($ tree = $ stmt- > fetch &#95; assoc()){
$ response [ “错误” ] =否;
$ response [ “ tree” ] [ “ treeid” ] = $ tree [ 'treeid' ];
$ response [ “ tree” ] [ “ treespecies” ] = $ tree [ 'treespecies' ];
$ response [ “ tree” ] [ “ treelatitude” ] = $ tree [ 'treelatitude' ];
$ response [ “ tree” ] [ “ treelongitude” ] = $ tree [ 'treelongitude' ];
echo json &#95; encode($ response),'<br>';
}
}其他 {
//找不到具有凭据的用户
$ response [ “错误” ] = TRUE ;
“ $ response [ ” error&#95; msg“ ] = ”树列表视图凭据错误。请重试!;
echo json &#95; encode($ response);
}
$ mysqli- >关闭();
}
答:使用以下代码使用mysqli和php从数据库中获取数据:
<?php
//创建连接
$ mysqli =新的mysqli(DB&#95; HOST,DB&#95; USER,DB&#95; PASSWORD,DB&#95; DATABASE);
//检查连接
如果($ mysqli-> connect&#95; error){
die(“连接失败:”。$ mysqli-> connect&#95; error);
}
$ stmt = $ mysqli-> query(“ SELECT * FROM tree where`condition` ='good'”);
如果($ stmt-> num&#95; rows> 0){
//输出每一行的数据
while($ tree = $ stmt-> fetch&#95; assoc()){
$ response [“错误”] =否;
$ response [“ tree”] [“ treeid”] = $ tree ['id'];
$ response [“ tree”] [“ treespecies”] = $ tree ['condition'];
$ response [“ tree”] [“ treelatitude”] = $ tree ['lat'];
$ response [“ tree”] [“ treelongitude”] = $ tree ['long'];
echo json&#95; encode($ response),'<br>';
}
}其他{
$ response [“ error”] = TRUE;
“; $ response [” error&#95; msg“] =”树列表视图凭据错误。请重试!“;
echo json&#95; encode($ response);
}
$ mysqli-> close();
?>
确保对DB_HOST,DB_user,DB_PASSWORD和DB_DATABASE使用正确的凭据。
我希望这将有所帮助。