【问题标题】:Array to String Conversion Error / PDO execute array in array数组到字符串转换错误/PDO 在数组中执行数组
【发布时间】:2014-11-25 17:54:42
【问题描述】:

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

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

$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]=> string(13) "示例标题" [1]= > string(14) "示例作者" } 注意:数组到字符串的转换在...."yadayadayada"

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

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

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

感谢任何帮助或指导:)

【问题讨论】:

  • 使用关联数组和命名占位符可能更容易理解。此外,尝试在数组中编译您的条件,然后加入 "AND" 作为最后一步。从头到尾都有双分支是大量的重复工作和自找麻烦。
  • 感谢@tadman 的提示,以后一定会这样做:)

标签: php mysql arrays pdo


【解决方案1】:

使用$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);

【讨论】:

  • 很好,解决了数组中数组的问题,并且有效。我同意,我的方法非常草率。感谢您提出更好的方法,我不知道存在 implode() 和 explode() 方法。必须更多地阅读文档。再次感谢。
  • 用 implode 很好地修复了那里。
猜你喜欢
  • 2014-03-29
  • 2014-09-26
  • 1970-01-01
  • 2017-05-27
  • 2015-02-26
  • 2012-07-15
  • 2016-08-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多