如何使用json_encode()获取PHP数组中的多行?
我有一个使用PHP/MySql的简单搜索表单。我遇到的问题是,我能够从数据库中获取一个字段,而不是第二个字段,即,如果我只传递数组中的第一个\u名称,但只要传递最后一个\u名称字段,它就会在搜索框中显示为“未定义” 我知道我在某个我无法进入的地方做错了什么。任何关于如何将这两个变量组合在一起的帮助都将不胜感激,因为我需要同时显示名字和姓氏。谢谢 这是我的密码 index.php如何使用json_encode()获取PHP数组中的多行?,php,mysql,arrays,Php,Mysql,Arrays,我有一个使用PHP/MySql的简单搜索表单。我遇到的问题是,我能够从数据库中获取一个字段,而不是第二个字段,即,如果我只传递数组中的第一个\u名称,但只要传递最后一个\u名称字段,它就会在搜索框中显示为“未定义” 我知道我在某个我无法进入的地方做错了什么。任何关于如何将这两个变量组合在一起的帮助都将不胜感激,因为我需要同时显示名字和姓氏。谢谢 这是我的密码 index.php <?php include('db.php'); ?> <!DOCTYPE html>
<?php
include('db.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="typeahead.min.js"></script>
<script>
$(document).ready(function(){
$('input.typeahead').typeahead({
name: 'typeahead',
remote:'search_bootstrap.php?key=%QUERY',
limit : 10
});
});
</script>
</head>
<body>
<input type="text" style="width: 200px;" name="typeahead" class="typeahead tt-query" autocomplete="off" spellcheck="false" placeholder="Type a name">
</body>
</html>
<?php
$key=$_GET['key'];
$array = array();
$con=mysql_connect("host","user","password");
$db=mysql_select_db("user",$con);
$query=mysql_query("select * from registered_users where first_name LIKE '%{$key}%'");
while($row=mysql_fetch_array($query))
{
$firstName = $row['first_name'];
$lastName = $row['last_name'];
array_push($array , array("first"=>$firstName, "last"=>$lastName));
}
echo json_encode($array);
?>
$(文档).ready(函数(){
$('input.typeahead')。typeahead({
名称:'typeahead',
远程:'search\u bootstrap.php?key=%QUERY',
限额:10
});
});
search\u bootstrap.php
<?php
include('db.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="typeahead.min.js"></script>
<script>
$(document).ready(function(){
$('input.typeahead').typeahead({
name: 'typeahead',
remote:'search_bootstrap.php?key=%QUERY',
limit : 10
});
});
</script>
</head>
<body>
<input type="text" style="width: 200px;" name="typeahead" class="typeahead tt-query" autocomplete="off" spellcheck="false" placeholder="Type a name">
</body>
</html>
<?php
$key=$_GET['key'];
$array = array();
$con=mysql_connect("host","user","password");
$db=mysql_select_db("user",$con);
$query=mysql_query("select * from registered_users where first_name LIKE '%{$key}%'");
while($row=mysql_fetch_array($query))
{
$firstName = $row['first_name'];
$lastName = $row['last_name'];
array_push($array , array("first"=>$firstName, "last"=>$lastName));
}
echo json_encode($array);
?>
你应该使用JSON.parse()你是说JSON.parse($array)?我试过了,如果你是这个意思的话,那没用。$lastName=$row['last_name'];]代码>应该是$lastName=$row['last_name']代码>我的坏,只是在上面编辑了它。我仍然在搜索框中未定义,但是如果我单独搜索_bootstrap.php,所有结果都在数组中。警告:如果您只是在学习php,请不要学习过时的界面。这很糟糕,已经在PHP7中删除了。替换类和指南类有助于解释最佳实践。请确保您的用户参数是正确的,因为这里有严重的错误。