用于自动完成jQueryUI的MYSQL到JSON
MYSQL to JSON for Autocomplete jQueryUI
下面的代码显示了我正在做的事情。基本上,目的是从数据库中检索数据,并将其转换为jQueryUI的JSON格式。通过下面的方法和一个不使用array_to_json函数的方法,我能够返回json数据。例如:[{"name":"Kurt Schneider"},{"name":"Sam Tsui"}
但问题是,它仍然不能与自动完成一起工作。我用示例中提供的search.php替换了下面的代码,所以这显然是该代码的一个问题。
我开始时只有这个代码:
<?php
include 'connect.php';
mysql_select_db("database", $con);
$search = mysql_query("SELECT name FROM artist");
$rows = array();
while($row = mysql_fetch_assoc($search)) {
$result[] = $row;
}
print json_encode($result);
?>
它实际上实现了与以下代码相同的结果(除了间距上的一些差异):(array_to_json函数是直接从示例中复制的)。
<?php
include 'connect.php';
mysql_select_db("database", $con);
$search = mysql_query("SELECT name FROM artist");
$rows = array();
while($row = mysql_fetch_assoc($search)) {
$result[] = $row;
}
function array_to_json( $array ){
if( !is_array( $array ) ){
return false;
}
$associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
if( $associative ){
$construct = array();
foreach( $array as $key => $value ){
// We first copy each key/value pair into a staging array,
// formatting each key and value properly as we go.
// Format the key:
if( is_numeric($key) ){
$key = "key_$key";
}
$key = "'"".addslashes($key)."'"";
// Format the value:
if( is_array( $value )){
$value = array_to_json( $value );
} else if( !is_numeric( $value ) || is_string( $value ) ){
$value = "'"".addslashes($value)."'"";
}
// Add to staging array:
$construct[] = "$key: $value";
}
// Then we collapse the staging array into the JSON form:
$result = "{ " . implode( ", ", $construct ) . " }";
} else { // If the array is a vector (not associative):
$construct = array();
foreach( $array as $value ){
// Format the value:
if( is_array( $value )){
$value = array_to_json( $value );
} else if( !is_numeric( $value ) || is_string( $value ) ){
$value = "'".addslashes($value)."'";
}
// Add to staging array:
$construct[] = $value;
}
// Then we collapse the staging array into the JSON form:
$result = "[ " . implode( ", ", $construct ) . " ]";
}
return $result;
}
echo array_to_json($result);
?>
<?php
include 'connect.php';
mysql_select_db("database", $con);
$search = mysql_query("SELECT name FROM artist");
$rows = array();
while($row = mysql_fetch_assoc($search)) {
//don't add the array, just the name.
$result[] = $row["name"];
}
print json_encode($result);
?>
上面的代码应该返回JSON
["Kurt Schneider", "Sam Tsui", "Christina Grimmie"]
相关文章:
- 使用mysql为JSON php数组分配多个列值
- 解析字符串包含'"'从mysql检索到JSON
- 如何使用php以json中的数组形式返回mysql-select
- 将MySQL中的JSON导入Google图表
- PHP json_encode() 将引号添加到从 MySQL 检索的 JSON 中
- 我有一个mysql的输入文本字段.那么,如何通过ajax从数据库中输出json表呢
- 无法使用jquery/ajax/json从mysql获取数据来选择框
- 如何在JavaScript中存储和检索MySQL数据库中的JSON数据
- 如何处理json编码的php-mysql数组
- node.js+MySQL;JSON结果-回调问题&没有响应客户端
- 从Node.js上的mysql结果JSON中获取一个Array值
- D3从mysql获取JSON数据,总是返回未定义的数据
- 使用php将Json数据存储到mysql数据库中
- Encoding Json from MySQL
- highchart mysql-json,带有where条件的下拉选择值
- 谷歌图表+ MySQL/json
- 从MySQL JSON输出渲染Highchart的问题
- 如何从数据库中检索值基于行id从url使用PHP mysql json
- PHP, MYSQL & JSON
- 仍然试图理解mysql json数据