views:

95

answers:

2

I am creating a class for an application "backbone" and I need this function to query the db, and return a multidimensional to look like this:

$myArray = ("name"=>"John", "dob"=>"January 5, 1955");

Of course the data for the array is from a database query. but, "name" and "dob" would be the database column name and "John" and "January 5, 1955" would be the value of the column

Here is my code:

    public function getFrame($id) {
    $getFrameQuery = "SELECT * FROM " . DB_FRAMETABLE . "WHERE `fhid`=" . $this->quote_smart($id);
    $getFrameRecord = $db->query_first($getFrameQuery);

}

Any help is greatly appreciated!

Josh

A: 

This isn't a multidimensional array. Just a regular associative array.

bonez
So the DB query returns a associative array?
josh brown
I am using this mysql class wrapper: http://www.ricocheting.com/scripts/php_mysql_wrapper.php
josh brown
Seems as if query_first() does return an associative array, the column names as keys. $getFrameRecord['name'] would get you "John"
voodoo555
$query = "select name from whatever";$results = mysql_query($query) or die("Query failed: ".mysql_error());while($row = mysql_fetch_assoc($results)){ echo $row['name'];}
bonez
+2  A: 

What database wrapper are you using? They usually provide a way to retrieve a row into an associative array.

mysql_fetch_assoc(), for example

From your comment to the other answer, the wrapper you are using provides a ->fetch_array() method that returns the data structure you are looking for.

Looks like you would need to change your code to use something like

$result = $db->query($getFrameQuery);
$data = $db->fetch_array($result);
jasonbar
http://www.ricocheting.com/scripts/php_mysql_wrapper.php
josh brown
thanks. What type of array did I use as an example for future reference?
josh brown
@josh brown: An associative array. http://php.net/manual/en/language.types.array.php
jasonbar
you used an associative or hash array, key => value A multidimensional array could have been (key => "value1", "value2")
bonez