数组到字符串转换错误/ PDO在数组中执行数组

时间:2014-11-25 17:54:43

标签: php mysql arrays pdo

所以我在使用PDO执行执行查询时遇到问题。我一直从服务器返回的错误是我有一个数组到字符串转换。然后通过告诉我在execute(array())代码行中输入的参数数量无效来跟进。我的错误代码中也有一个1缓冲区,这意味着可能存在一些错误的连接。

该功能是一个搜索页面,用户最多可输入6个参数来搜索图书。有6个if语句用于检查emptys,如果不为空,则将变量添加到数组params,并将部分sql查询连接到begin语句的末尾。以下是相关代码:

$title = $_POST['title'];
$author = $_POST['author'];
$publisher = $_POST['publisher'];
$isbn = $_POST['isbn'];
$condition = $_POST['condition'];
$status = $_POST['status'];

//array to hold variables
$params = array();
$concat_stmt_orig = "SELECT * FROM Book WHERE ";
$concat_stmt = "SELECT * FROM Book WHERE ";

//Check for Title
if(!empty($title)){
    //add title to the end
    $concat_stmt = $concat_stmt." `Title` = ?";
    //add variable to print list
    array_push($params, $title);

}
//Check for Author
if(!empty($author)){
    if(strlen($concat_stmt) !== strlen($concat_stmt_orig)){
        //add author to the end
        $concat_stmt = $concat_stmt." AND `Author1` = ?";
        //add variable to print list
        array_push($params, $author);
    }
    else{
        //add author to the end
        $concat_stmt = $concat_stmt." `Author1` = ?";
        //add variable to print list
        array_push($params, $author);
    }
}

//Check for publisher
if(!empty($publisher)){
    if(strlen($concat_stmt) !== strlen($concat_stmt_orig)){
        //add publisher to the end
        $concat_stmt = $concat_stmt." AND `Publisher` = ?";
        //add variable to print list
        array_push($params, $publisher);
    }
    else{
        //add pubisher to the end
        $concat_stmt = $concat_stmt." `Publisher` = ?";
        //add publisher to print list
        array_push($params, $publisher);
    }
}
//check for ISBN
if(!empty($isbn)){
    if(strlen($concat_stmt) !== strlen($concat_stmt_orig)){
        //add isbn to the end
        $concat_stmt = $concat_stmt." AND `ISBN` = ?";
        //add variable to print list
        array_push($params, $isbn);
    }
    else{
        //add pubisher to the end
        $concat_stmt = $concat_stmt." `ISBN` = ?";
        //add publisher to print list
        array_push($params, $isbn);
    }
}
//check for condition
if(!empty($condition)){
    if(strlen($concat_stmt) !== strlen($concat_stmt_orig)){
        //add condition to the end
        $concat_stmt = $concat_stmt." AND `BookCondition` = ?";
        //add condition to print list
        array_push($params, $condition);
    }
    else{
        //add condition to the end
        $concat_stmt = $concat_stmt." `BookCondition` = ?";
        //add condition to print list
        array_push($params, $condition);
    }
}

//check for status
if(!empty($status)){
    if(strlen($concat_stmt) !== strlen($concat_stmt_orig)){
        //add status to the end
        $concat_stmt = $concat_stmt." AND `Status` = ?";
        //add variable to print list
        array_push($params, $status);
    }
    else{
        //add condition to the end
        $concat_stmt = $concat_stmt." `Status` = ?";
        //add condition to print list
        array_push($params, $status);
    }
}

 //prepare statement for querying
 $stmt_retrieve = $db->prepare($concat_stmt);

 echo print_r($stmt_retrieve);
 var_dump($params);

 //exectue query
 $stmt_retrieve->execute(array($params));
}

echo和var dump用于测试目的,以检查sql语句和正确数量的数组值

以下是错误

  

PDOStatement对象([queryString] => SELECT * FROM Book WHERE Title =?AND Author1 =?)1array(2){[0] =>字符串(13)"示例标题" [1] =>字符串(14)"示例作者" }   注意:数组到字符串转换在...." yadayadayada"

     

致命错误:未捕获的异常' PDOException'消息' SQLSTATE [HY093]:参数号无效:绑定变量数与令牌数不匹配' ...." yadayadayada"

我尝试将帖子值输入' $ title',' $ author' ...等但仍然产生了错误,我也试过了printf和其他人

当我尝试输入多个变量进行搜索时,我只会收到这些错误。当我只搜索一个变量时,程序没有错误,但仍然无法返回任何内容。

感谢任何帮助或指导:)

1 个答案:

答案 0 :(得分:2)

使用$stmt_retrieve->execute($params);

目前,您拥有相应的:

$stmt_retrieve->execute(array(array('title','author1',....)));

在那里看到双array

@tadman是对的,你当前的代码有一个大量的代码重复,很快变得无法维护。

这样的事情会有所帮助:

$fields = array(
   'Title'   => $_POST['title'],
   'Author1' => $_POST['author'],
   ....);

$params = array();
$wheres = array();

foreach($fields as $fieldname => $value){
    if(!empty($value)){
        $wheres[] = "`$fieldname` = ?";
        $params[] = $value;
    }
}
$stmt = 'SELECT * FROM Book';
if(!empty($wheres)) $stmt .= ' WHERE '.implode(' AND ',$wheres);
$stmt_retrieve = $db->prepare($stmt);
$stmt_retrieve->execute($params);