0

我正在尝试从数据库中获取数据,问题是我收到通知'Array to string conversion'$array返回“数组”。该get_rows方法使用当前查询获取所有行。知道为什么会这样吗?这是代码:

public function load_seat_by_number($seat_number)
    {
        $db = model_database::instance();
        $sql = "SELECT * FROM `seats` WHERE `seat_number` = '". intval($this->seat_number). "'";
        if (($array = $db->get_rows($sql)) > 0)
            return $array;
        else return FALSE;
    }
4

3 回答 3

1

This happens because $array is, well, an array. Use count instead. Otherwise, check your database model to see if there is a way to get the number of rows from the result.

$array = $db->get_rows($sql);
if(count($array) > 0)
于 2013-09-19T12:37:20.637 回答
0

使用sizeoforcount获取数组中元素的数量。

if (($array = sizeof($db->get_rows($sql))) > 0) //returns the integer value(total no. of elements in an array)
        return $array;

或者

 if (($array = count($db->get_rows($sql))) > 0)  //returns the integer value(total no. of elements in an array)
            return $array;
于 2013-09-19T12:43:59.257 回答
0

这应该工作

public function load_seat_by_number($seat_number)
{
    $db = model_database::instance();
    $sql = "SELECT * FROM `seats` WHERE `seat_number` = '". intval($this->seat_number). "'";
    if (count($db->get_rows($sql)) > 0)
        return $db->get_rows($sql);
    else return FALSE;
}
于 2013-09-19T12:44:33.467 回答