Think I could use some fresh eyes on this one. I’m sure this is how I’ve done it before but I cannot access the results of my SQL query properly.
I’m using this piece of code in a function to retrieve certain category information. In this particular instance it is calling this:
SELECT * FROM categories WHERE type IN (‘category’) ORDER BY category_name ASC
$query = "SELECT $columns FROM categories WHERE $where $orderby $order $limits";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
$catList[] = $row;
return $catList;
I tried just returning row but it only returned one record. Returning result just has it contain “mysql resource(#)”.
But I can’t seem to access the information properly, saying any I try to access are an undefined index.
foreach($categories as $category) {
if($categories['category_id'] = $selected) {
$output .= "\ <option value='".$categories['category_id']."'$selected>".$categories['category_name']."</option>\
";
break;
}
}
A var_dump of $categories results in the following (restricted to one entry for readability)
array(1) {
[0]=> array(14) {
[0]=> string(1) "3" ["category_id"]=> string(1) "3"
[1]=> string(2) "1a" ["category_name"]=> string(2) "1a"
[2]=> string(5) "test1" ["cleanname"]=> string(5) "test1"
[3]=> string(8) "category" ["type"]=> string(8) "category"
[4]=> string(3) "123" ["description"]=> string(3) "123"
[5]=> string(1) "1" ["parent"]=> string(1) "1"
[6]=> string(1) "5" ["count"]=> string(1) "5" } }
I don’t know if being an array within an array is at issue, I assumed that made sense as it obviously separates individual records. Echoing anything but category_id returns “Array” though I can’t see any nested arrays. Echoing category_id just returns ‘1’ regardless of the actual id of the returned record.
EDIT: Removing makes it at least accessible but only returns 1 record and it still returns the wrong id or at least not returns it as much as when it is being echo’d only ‘1’ appears.
EDIT2: Discovered at least part of the problem. Somewhere along the line, the default category (currently 1) is being assigned to category_id. Found it, changed = to ===, fixed that issue at least. Now just need to make it return the sql results properly.