0

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;
    }
1
  • This intval($this->seat_number) should just be intval($seat_number), no? Commented Sep 19, 2013 at 12:35

3 Answers 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)
Sign up to request clarification or add additional context in comments.

5 Comments

I tried, but it still returns 'Array'. $array = $db->get_rows($sql); if(count($array) > 0) return $array; else return FALSE;
What do you expect it to return?
I want to return the data in the rows of the database. I'm sorry if I'm being noobish, but I'm really a beginner.
@icarus Then you need to echo what you need to come back, something to theh affect of echo $get_rows['seat_number']; --- return $array doesn't show the results in the array.
Your data is in the array. You'll need to iterate through the array to get the data. Use foreach (php.net/manual/en/control-structures.foreach.php) to iterate through the array, though each one of those will be another array with the columns of each row.
0

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;

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.