mardi 2 février 2016

How to make an object associative array in php

I am trying to select information from a db and store the information in an associative array. I have it working so far as to select the information and make an object which is stored in the array however I can't get the association to work. I want each book to be associated with the title.

When I try to push the association onto the array it is creating an array within an array.

public function getBookList()
    {
        $conn = mysqli_connect('localhost', 'root', '') or die ('No connection');
        mysqli_select_db($conn, 'books' ) or die (' database will not open');
        $query = "select title, author, description from book";  
        $result = mysqli_query($conn, $query) or die("Invalid query"); // runs query using
        // open connection

        $count = 0;
        $booksArray= array();
        while($row = mysqli_fetch_row($result))//  while there are rows available
        {

            $booksArray[$count] = new Book($row[0],$row[1],$row[2]);
            $count++;

            array_push($booksArray, array($row[0] => $tempBook));           

        }

        return($booksArray);
        mysqli_close($conn);
    }




Aucun commentaire:

Enregistrer un commentaire