php - PDOStatement->bindParam 不起作用

标签 php mysql database pdo

我在使用 $query->bindParam 时遇到了一些问题。它不会将占位符更改为值。

$options 是一个数组:

$options['where'] = [
    'id'    => [ 1, 2, 3 ],
    'title' => [ 'some', 'title' ]
];
$fields = '*';
$where_clauses = []; /* define empty error which could be filled with where clauses */
/* if it is a separte blog, only posts posted by it's admin should be got */
if ( Main::$current_blog_id !== 1 )
    $where_clauses[] = '`author_id` IN(' . $this->blog->admin_id . ')';

/* if options have been passed */
if ( ! empty( $options ) ) {
    /* if there are "where" options */
    if ( ! empty( $options['where'] ) ) {
        $placeholder_number = 1; /* define placeholder number */
        $placeholders       = []; /* create array with placeholders 'placeholder_number' => 'value' */
        foreach ( $options['where'] as $field_name => $values ) {
            $field_values = []; /* values that will be used to create "IN" statement */

            /* fill $fild_values array with values */
            foreach ( $values as $value ) {
                $placeholder = ':' . $placeholder_number; /* create placeholder */

                $field_values[] = $placeholder; /* add placeholder to field values */

                $placeholders[ $placeholder ] = $value; /* add placeholer with it's value to common placeholders array */

                $placeholder_number++; /* increase placeholder number */
            }

            /* turn $fields_values array into string */
            $field_values = implode( ', ', $field_values );

            /* create clause and put it into $where_clauses */
            $where_clauses[] = '`' . $field_name . '` IN(' . $field_values . ')';
        }
    }
}

/* if where statement is empty */
$where_clauses =
    ! empty( $where_clauses ) ?
    implode( ' AND ', $where_clauses ) :
    1;

$query = Main::$data_base->pdo->prepare(
    'SELECT ' . $fields . ' ' .
    'FROM `posts` ' .
    'WHERE ' . $where_clauses . ' ' .
    'LIMIT ' . $posts_quantity . ';'
);

/* if there are placeholders in the query */
if ( ! empty( $placeholders ) ) {
    foreach ( $placeholders as $placeholder => $value )
        $query->bindParam( $placeholder, $value, PDO::PARAM_STR );
}

$query->execute();
$posts = $query->fetchAll( PDO::FETCH_ASSOC );

var_dump( $query );

return
    ! empty( $posts ) ?
    $posts :
    [];

完成所有这些之后,我打印的查询如下所示:

SELECT * FROM `posts` WHERE `id` IN(:1, :2, :3) AND `title` IN(:4, :5) LIMIT 15;

最佳答案

您需要像这样定义绑定(bind)

 Array ( ':1' => 1, ':2' => 2, ':3' => 3, ':4' => 'some', ':5' => 'title' )

另外你的选择不正确...

"SELECT * FROM posts WHERE id IN(:1, :2, :3) AND title IN(:4, :5) LIMIT 15;"

你必须在里面放 AND 而不是 &&。

关于php - PDOStatement->bindParam 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44869216/

相关文章:

php - 使用 mysql 在矩形或圆中查找点

database - 如何最好地表示数据库中具有可变属性的项目?

java - 如何在我的数据库中插入复合组件(菜单菜单)

php - 查询 Laravel 深夜营业时间

mysql - 除了字符串的第一个字符之外,如何在 mysql 中区分大小写?

php - 从缺少某些 WHERE 条件的表中选择

database - 数据库中的可恢复性和级联调度

php - 无法在 CakePHP 的内部连接中获取第二个表值

php - 在 PHP 中使用 preg_replace() 中的每个匹配项

php - 如何迭代页面上的表单字段并将它们发送到数据库?