无法从MySQL恢复值

时间:2013-08-30 23:17:35

标签: php autocomplete jquery-autocomplete

我有以下代码,与test2.php有关。我的问题是我必须将菜单中选择的用户与其唯一ID相关联,因此URL将是所选用户的页面。问题是,id是未定义的。

我不知道为什么!我究竟做错了什么?来自

的网址
window.location.href = '/profile.php?id='+pieces[1];

/profile.php?id=undefined

$("#course").autocomplete("/test/test2.php", {
        selectFirst: false,
        formatItem: function(data, i, n, value) {
            //make the suggestion look nice
            return "<font color='#3399CC'>" + value.split("::")[0] + "</font>";
        },
        formatResult: function(data,value) {
            //only show the suggestions and not the URLs in the list
            return value.split("::")[0];
     var username = splitItems[0];
         var id = splitItems[1];
          return username;
        }
    }).result(function(event, data, formatted) {
         //redirect to the URL in the string
    var pieces = formatted.split("::");
        window.location.href = '/profile.php?id='+pieces[1];

test2.php

<?php
mysql_connect ("****", "****","****")  or die (mysql_error());
mysql_select_db ("****");
$q = strtolower($_GET["q"]);
if (!$q) return;
$sql = "select Username, id from **** where Username LIKE '%$q%'";
$rsd = mysql_query($sql);
while($rs = mysql_fetch_array($rsd)) {
    $cname = $rs['Username'];
    echo "$cname\n";
}
?>

2 个答案:

答案 0 :(得分:1)

我认为你的脚本中的/test/test2.php实际上不包含并运行test2.php。

试试这个

$("#course").autocomplete("<?php include '/test/test2.php'; ?>", {

答案 1 :(得分:1)

对于您的第一个代码,您应该将其替换为:

$("#course").autocomplete("/test/test2.php", {
        delay: 0,
      selectFirst: false,
        formatItem: function(data, i, n, value) {
        //make the suggestion look nice
        var pièces = value.split("::");
              return "<font color='#000000'; font size='3pt'; font face='Arial'>" + pièces[0]  + "</font>";
        },
        formatResult: function(data,value) {
            //only show the suggestions and not the URLs in the list
        var pièces = value.split("::");

                return pièces[0];

        }
    }).result(function(event, data, formatted) {
    //redirect to the URL in the string

    var pieces = formatted.split("::");
          window.location.href = '/profile.php?id='+pieces[1];

第二段代码

你应该添加:

$id = $rs['id'];

$cname = $rs['Username'];

并将此更改添加到以下行。

echo (utf8_encode("$cname::$id\n"));