![]() |
unusual behavior (database class)
I am getting an odd behavior with my database class. Here's what my fetch method looks like.
PHP Code:
In my main script. If I assign the object to a variable, I get the correct output. My table only has 1 record so I get this. PHP Code:
Key: 0 AND Value: Cleo But if I do a foreach on the "$row" variable, I get a whole bunch of data... PHP Code:
My output is: Quote:
Shouldn't the output be just like the first output? Just one row? Key: 0 AND Value: Cleo ???? Also, the "mysql_num_rows" count comes back as 1, which is correct because there's only 1 record in the table.. ONE MORE THING.... I did a little troubleshooting... When I do a count on "$rows": $countme = count($rows); I get back: 10 BUT, in my database class, I get just 1 back from this: $count = $db->num_rows(); And the method looks like this: PHP Code:
|
It is a personal preference of mine, but I prefer to avoid switch statements when possible so I thought perhaps you would share this same bias and maybe prefer this way.
PHP Code:
|
That is the correct behaviour insofar as I can tell. You should really have a
fetchRow and fetchAll function.Having an ambiguous fetch function doesn't imply either one nor the other. That function would make a good fetchAll, but for a fetchRow, try using mysql_fetch_row and returning that. |
Quote:
Quote:
As for your "ONE MORE THING...." with regards to counting the return value of your method. You are counting the number of array items returned. Since the array only holds one row, the count is twice the number of columns (since both number and associative indices are being returned) in the table, not the number of rows returned by the query. |
Quote:
fetchRow and fetchAll, and the return is based on an actual item instead of many items contained within an array, so to speak. |
I guess I've been using fetch_array and fetch_assoc so much I kinda forgot how they worked exactly.
Here's how I normally access a table. Hypothetical Table table_id,name,type,status So my "procedural" way of getting the data is like so: PHP Code:
$db->query("SELECT * FROM table"); $db->num_rows(); $db->fetch('row'); //pass 'row' as the parameter to access mysql_fetch_row function.... for($i=0;$i<$db->num_rows();<$i++) { $rows = $db->fetch('row'); $table_id = $rows['table_id']; $name = $rows['name']; $type = $rows['type']; $status = $rows['status']; echo "$table_id, $name, $type, $status <br />"; DOESNT WORK-----^ } So now how do I actually access the values in the returned rows like I did the procedural way? Sorry, my transition from proc to OO is a little shaky... |
| All times are GMT. The time now is 07:45 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0