数组获取空集时出错

时间:2012-04-13 01:06:32

标签: php jquery arrays json

我在框中输入内容后,在php文件输出中更新了问题: print_r($_POST);

Array
(
    [q] => running
)

打印json_encode($jsonArray);但是正在打印[]

php查询Interest表中的绑定参数

    $interestValue = $_POST['interestVal']; //////
    $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  
    $sth = $dbh->prepare(
'SELECT interestID, interestVal FROM Interest WHERE interestVal = ?');
    $sth->bindParam(1, $interestValue);
    $sth->execute();
    $jsonArray = array();
    while ($result = $sth->fetch(PDO::FETCH_ASSOC)) {
     $jsonArray[] = array(
      'ID' => $result['interestID'], 
          'Value' => $result['interestVal']); 
    }

    print_r($_POST);

    print json_encode($jsonArray); // json encode that array 

HTML

<input id="interest" name="interest" value="What are your interests?" />

JS

//Get interests
        $(document).ready(function() {
            $("input#interest").tokenInput("../../src/php/registration/interest/getInterest.php");
        });

框架的JS(http://loopj.com/jquery-tokeninput/)

(function ($) {
// Default settings
var DEFAULT_SETTINGS = {
    // Search settings
    method: "POST",
    contentType: "json",
    queryParam: "interestVal",
    searchDelay: 300,
    minChars: 1,
    propertyToSearch: "name",
    jsonContainer: null,

    // Display settings
    hintText: "Type in a search term",
    noResultsText: "No results",
    searchingText: "Searching...",
    deleteText: "&times;",
    animateDropdown: true,

    // Tokenization settings
    tokenLimit: null,
    tokenDelimiter: ",",
    preventDuplicates: false,

    // Output settings
    tokenValue: "name", // orig "id"
}

为什么这个数组是空的?

1 个答案:

答案 0 :(得分:1)

不要将mysql_real_escape_string与预准备语句一起使用 - 绑定参数将用户数据从SQL代码中删除,因此没有SQL注入漏洞。更重要的是,mysql_real_escape_string仅在您连接到数据库之后才有效,而不是之前,这会将您的值转换为false