简体   繁体   English

PHP中的数组到字符串转换的通知

[英]Array to string conversion notice in PHP

I am trying to display records using PDO LIKE query, but I am getting this error message can I know how to solve this. 我正在尝试使用PDO LIKE查询显示记录,但是我收到此错误消息,我知道该如何解决。

This is my code: 这是我的代码:

 $rs = new JSONRecordSet();
 $searchbooksSQL = "SELECT Title FROM l_stock WHERE Title LIKE ?";
 $params = array("%$term%");
 echo $rs->getRecordSet($searchbooksSQL, $params);

This is the getRecordSet code: 这是getRecordSet代码:

class R_RecordSet {
    function getRecordSet($sql, $params = null) {
        if (is_array($params)) {
            $this->stmt = $this->db->prepare($sql);
            // execute the statement passing in the named placeholder and the value it'll have
            $this->stmt->execute($params);
        } else {
            $this->stmt = $this->db->query($sql);
        }
        return $this->stmt;
    }
}

class JSONRecordSet extends R_RecordSet {
    function getRecordSet($sql, $elementName = "ResultSet", $params = null) {
        $stmt = parent::getRecordSet($sql, $params);
        $recordSet = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $nRecords = count($recordSet);
        if ($nRecords == 0) {
            $status = 'error';
            $message = json_encode(array("text" => "No records found"));
            $result = '[]';
        } else {
            $status = 'ok';
            $message = json_encode(array("text" => ""));
            $result = json_encode($recordSet);
        }
        return "{\"status\": \"$status\", \"message\":$message, \"$elementName\" :{\"RowCount\": $nRecords ,\"Result\": $result}}";
    }
}

The error message i am getting is "Notice: Array to string conversion" 我收到的错误消息是“注意:数组到字符串的转换”

getRecordSet() is defined as: getRecordSet()定义为:

 function getRecordSet($sql, $elementName = "ResultSet", $params = null) { 

however, you are calling it as: 但是,您称其为:

 echo $rs->getRecordSet($searchbooksSQL, $params); 

You will need to modify your code to pass in an appropriate $elementName parameter. 您将需要修改代码以传递适当的$elementName参数。 (The default is probably reasonable.) (默认值可能是合理的。)

 echo $rs->getRecordSet($searchbooksSQL, 'ResultSet', $params);


Additionally, you should probably use json_encode() to generate the final result from JSONRecordSet::getRecordSet() , rather than building it up with string concatenation. 另外,您可能应该使用json_encode()JSONRecordSet::getRecordSet()生成最终结果,而不是使用字符串串联来构建最终结果。 It will make the code easier to read and understand. 这将使代码更易于阅读和理解。

Also, your two implementations of getRecordSet() are incompatible with each other, according to the Liskov Substitution Principle due to the change in the semantics of the input parameters, and is likely what led you to the parameter mismatch at your call site in the first place. 另外,由于输入参数的语义变化,根据Liskov替换原理getRecordSet()两个实现彼此不兼容,这很可能是导致第一个调用站点的参数不匹配的原因地点。 You probably want to re-order JSONRecordSet::getRecordSet() parameters to: 您可能想将JSONRecordSet::getRecordSet()参数重新排序为:

function getRecordSet($sql, $params = null, $elementName = 'ResultSet') {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM