php mysql ajax 实时搜索数组

标签 php mysql ajax arrays

我有一个 ajax 实时搜索脚本,但它似乎只返回数组的第一行。不知道这是否与我的循环有关,但是,如果有人能发现错误,将非常感谢帮助,因为我似乎找不到它。我不包括 javascript,因为我很确定这不是错误,因为 php 文件正在触发。它只是回响第一个点击,而不是迭代其他点击。

//run query on dbase then use mysql_fetch_array to place in array form
while($a = mysql_fetch_array($res,MYSQL_BOTH))
{
    //lookup all hints from array if length of q>0
    if (strlen($q) > 0)
    {
        $hint="";
        for($i=0; $i<count($a); $i++)
        {
            if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
            {
                $n = $a['first']." ".$a['last'];
                if ($hint=="")
                {
                    $hint='<a href="mailto.php">'.$n.'</a>'; 
                }
                else
                {
                    $hint=$hint.'<br><a href="mailto.php">'.$n.'</a>';// we do not seem to get here
                }
            }
        }
    }

// Set output to "no suggestion" if no hints were found
// or to the correct values
}//close while fetch
echo $hint;

最佳答案

您将在每次 while 循环迭代中覆盖您的 $hint 变量。尝试在 while 之前声明它(删除现在拥有它的位置)

$hint = "";
while($a = mysql_fetch_array($res,MYSQL_BOTH))
{

关于php mysql ajax 实时搜索数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10933266/

相关文章:

mysql - 如果其中一行包含空列,则合并两行

jquery - 使用 AJAX 初始化数据表后使用 .row.add()

javascript - 添加远程 : => true on Form_for

java - 通过 ajax 调用调用操作方法时出现意外行为

php - Linux 用户创建 + PHP 和电子邮件

php - 函数内部的数据库查询未发布到页面

PHP 将 KB MB GB TB 等转换为字节

java - Android 连接 MySQL 数据库失败

php - 没有PHP错误,codeigniter仍然显示空白页

php - 在 Laravel 5 Collection 中,如何返回对象数组而不是数组数组?