我创建了这个功能:
<?php
$pdo_db = new PDO( 'mysql:host=' . $db_host . ';dbname=' . $db_name, $db_user_name, $db_password );
function qlist_pdo( $fields, $tables, $where_fields='', $where_values='', $order='', $limit='' ) {
GLOBAL $pdo_db; // ignore this, it will be in class later. This is just for test purposes
if ( $order ) $order = ' ORDER BY ' . $order;
if ( $limit ) $limit = ' LIMIT ' . $limit;
if ( is_array( $where_fields ) && is_array( $where_values ) && isset( $where_fields ) && isset( $where_values ) ) {
if ( sizeof( $where_fields ) != sizeof( $where_values ) ) {
die( "Query error: where <strong>field</strong> count doesn't match <strong>value</strong> count!" );
} else {
$where = ' WHERE ';
foreach( $where_fields as $key => $value ) {
$where .= $key . ' ' . $value . '=? ';
}
$where = substr( $where, 0, -1 );
echo $query = "SELECT " . $fields . " FROM " . $tables . $where . $order . $limit;
$qp = $pdo_db -> prepare( $query );
foreach ( $where_values as $key => $val ) {
$qp -> bindParam( $key+1, $val );
}
}
} else {
echo $query = "SELECT " . $fields . " FROM " . $tables . $order . $limit;
$qp = $pdo_db -> prepare( $query );
}
$qp -> execute();
$result = $qp -> fetchAll();
return $result;
}
?>
我称之为:
$result = qlist_pdo( "*", "list", array( '' => 'id', 'OR' => 'id' ), array( '2', '8' ), 'id DESC' );
它工作正常,但是当我设置where参数时,结果只给出了一行(它应该是2)。如果我没有像这样的参数调用它:
$result = qlist_pdo( "*", "list", '', '', 'id DESC' );
返回所有行。为什么这样,我做错了什么?
生成查询:
SELECT * FROM list WHERE id=? OR id=? ORDER BY id DESC
生成的参数:
1, 2
2, 8
[编辑] 看起来它只添加第二个参数。如果我将OR
更改为AND
,它仍会提供一个结果,即使它根本不应该提供任何行(因为不能有ID = 2 AND ID = 8选项)。
[EDIT2] 我设置了查询日志,得到了我的想法。实际查询为SELECT * FROM list WHERE id='8' OR id='8' ORDER BY id DESC
答案 0 :(得分:0)
您将参数作为字符串传递,但它们是整数。
我建议以不同的方式传递数组:
$result =
qlist_pdo( "*", "list",
array( '' => 'id', 'OR' => 'id' ), array( 2, 8 ), 'id DESC' );
您的功能应如下所示:
function qlist_pdo( $fields, $tables, $where_fields = '', $where_values = '', $order = '', $limit = '' )
{
if ( $order )
$order = ' ORDER BY ' . $order;
if ( $limit )
$limit = ' LIMIT ' . $limit;
if ( is_array( $where_fields ) && is_array( $where_values ) && isset( $where_fields ) && isset( $where_values ) )
{
if ( sizeof( $where_fields ) != sizeof( $where_values ) ) {
die( "Query error: where <strong>field</strong> count doesn't match <strong>value</strong> count!" );
}
else {
$where = ' WHERE ';
$p = 0;
foreach ( $where_fields as $key => $value ) {
$where .= $key . ' ' . $value . '= :p' . $p . ' ';
$p++;
}
$where = substr( $where, 0, -1 );
echo $query = "SELECT " . $fields . " FROM " . $tables . $where . $order . $limit; // . '<br>';
$qp = $pdo_db->prepare( $query );
foreach ( $where_values as $key => $val ) {
$qp -> bindParam( ":p$key", $val, PDO::PARAM_INT );
//echo ":p$key" . '<br>';
}
}
}
else {
echo $query = "SELECT " . $fields . " FROM " . $tables . $order . $limit;
$qp = $pdo_db->prepare( $query );
}
}
当删除输出的注释时,我得到了这个结果(对我来说似乎没问题)
SELECT * FROM list WHERE id= :p0 OR id= :p1 ORDER BY id DESC
:p0
:p1
答案 1 :(得分:0)
而不是:
foreach ( $where_values as $key => $val ) {
我需要:
foreach ( $where_values as $key => &$val ) {
不知道为什么,但&amp; 是我需要添加到代码中的唯一内容。