在mysqli param bind中使用数组

时间:2015-08-11 10:22:36

标签: php mysqli

我收到了PHP警告:

  

mysqli_stmt_bind_param()期望参数2为字符串,给定数组。有问题的一行是: mysqli_stmt_bind_param($ stmt,$ types,$ values);

我想这是因为$types$values是包含绑定数据的数组,问题是我不知道如何将它们从数组中导入绑定命令。

使用基于程序的代码对此有何帮助?

   if (isset($_POST['submit'])) {

    $query = "SELECT profile FROM profiles WHERE state != 9 AND blocked = 0";
    $conditions = [];
    $lookingfor = [];

// Validate minimum age
    if ((isset($_POST['minimumage'])) and (preg_match('/^\d{2}$/', $_POST['minimumage']))) {
        $conditions['minage >= ?'] = ['i', $_POST['minimumage']];
        $lookingfor[] = "Minimum age " . $_POST['minimumage'];
    }

// Validate maximum age
    if ((isset($_POST['maximumage'])) and (preg_match('/^\d{2}$/', $_POST['maximumage']))) {
        $conditions['maxage <= ?'] = ['i', $_POST['maximumage']];
        $lookingfor[] = "Maximum age " . $_POST['maximumage'];
    }

    if (!empty($lookingfor)) {
        foreach ($lookingfor as $var) {
            echo "<li>" . $var . "</li>";
        }
    }

    $where_clauses = $types = $values = [];

    if (!empty($conditions)) {
        foreach ($conditions as $clause => $condition) {
            $where_clauses[] = $clause;
            list($types[], $values[]) = $condition;
        }
        $query .= " AND ";
        $query .= join(" AND ", $where_clauses);
    }

    echo $query;

    $stmt = mysqli_prepare($sql_connect, $query);

    if (!empty($conditions)) {
        mysqli_stmt_bind_param($stmt, $types, $values);
    }
    mysqli_stmt_execute($stmt);
    mysqli_stmt_store_result($stmt);
    mysqli_stmt_fetch($stmt);

    if (mysqli_stmt_num_rows($stmt) > 0) {
        echo "Found " . mysqli_stmt_num_rows($stmt) . " profiles which match this criteria";
    }
}

0 个答案:

没有答案