Hai
Hai

Reputation: 13

Get all data from sql php

I want to get all locations in table travel_location in mysql. This is my code

$select = $this->_db_table->select()->from(travel_location, array('*'));

    $result = $this->_db_table->fetchAll($select);


    if(count($result) == 0) {
        throw new Exception('not found',404);
    }

    while ($row1 = mysql_fetch_array($result)){
        $user_object = new Api_Model_User($row1);

        $count = 1;

        $json = array($json[$count] = array(
        'travel_location_id' => $user_object->travel_location_id,
        'city_id' => $user_object->city_id,
        'user_id' => $user_object->user_id,
        'location_name' => $user_object->location_name,
        'description' => $user_object->description,
        'longitude' => $user_object->longitude,
        'latitude' => $user_object->latitude,
        'created_time' => $user_object->created_time,
        'updated_time' => $user_object->updated_time));

        $count++;
    }

It doesn't work. I print $row1 = mysql_fetch_array($result) and it returns false, so I think it's wrong because of this line. How can I fix it?

Upvotes: 0

Views: 164

Answers (1)

bitWorking
bitWorking

Reputation: 12675

If you use fetchAll from Zend_Db_Table you get a Zend_Db_Table_Rowset as result.

Try this:

foreach ($result as $row) {
    // $row should be a Zend_Db_Table_Row object
    // you can cast to array
    $rowArray = $row->toArray();
    $user_object = new Api_Model_User($rowArray);
}

Read more about here and here

Upvotes: 1

Related Questions