icarus
icarus

Reputation: 166

php getting data from mysql database

I'm trying to get data fom a database, the problem is that I get a notice 'Array to string conversion', and $array returns 'Array'. the get_rows method gets all the rows using the current query. Any idea why this is happening? here's the code:

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;
    }

Upvotes: 0

Views: 91

Answers (3)

Adam
Adam

Reputation: 1371

This should work

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;
}

Upvotes: 0

user1850534
user1850534

Reputation:

Use sizeof or count to get the number of elements in an array.

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

or

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

Upvotes: 0

aynber
aynber

Reputation: 23000

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)

Upvotes: 1

Related Questions