无法从MySQL恢复值

I have the following code, that is related to test2.php. My problem is that I have to link the user selected in the menu with their unique ID, so the URL will be the page of the user selected. The problem, is that the id is undefined.

And I don't have any idea why! What am I doing wrong? The URL that come from

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

is /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
";
}
?>

For your first code, you should replace it by this one:

$("#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];

For your second code

You should add:

$id = $rs['id'];

Under

$cname = $rs['Username'];

and add this changes to the following line.

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

I don't think putting /test/test2.php in your script actually includes and run test2.php.

Try this

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