|
Posted by Jerry Stuckle on 04/20/07 14:34
Karl Groves wrote:
> Jerry Stuckle <jstucklex@attglobal.net> wrote in
> news:VpudnVRfZbwINbXbnZ2dnUVZ_uDinZ2d@comcast.com:
>
>> Karl Groves wrote:
>>> I'm missing something very obvious, but it is getting late and I've
>>> stared at it too long.
>>>
>>> TIA for responses
>>>
>>>
>>> I am writing a basic function (listed at the bottom of this post)
>>> that returns data from a query into an array.
>>>
>>> The intent is that the following code:
>>> $foo = dbSelectData("SELECT foo, bar FROM table", $link);
>>> would return an array with the keys: 'foo' and 'bar'.
>>>
>>> But what I get is instead a multidimensional array.
>>> Doing a var_dump on $foo turns out to be
>>> array(2) {
>>> ["foo"]=>
>>> string(5) "Stuff"
>>> ["bar"]=>
>>> string(10) "More Stuff"
>>> }
>>>
>>>
>>> I'm wondering where I went wrong in writing the function below.
>>>
>>>
>>> function dbSelectData($query, $connection, $rtype=MYSQL_ASSOC){
>>>
>>> $result = mysql_query($query, $connection);
>>> if(!$result){
>>> dbThrowError("Error in function dbSelectData. Query Was
>>> <em>
>>> $query</em>.");
>>> return FALSE;
>>> }
>>> else{
>>> $numrows = mysql_num_rows($result);
>>>
>>> if($numrows == 0){
>>> return FALSE;
>>> }
>>> else{
>>> while($rows = mysql_fetch_array($result, $rtype)){
>>> $output[] = $rows;
>>> }
>>> mysql_free_result($result);
>>> return $output;
>>> }
>>> }
>>> }
>>>
>>>
>>> Thanks in advance.
>>>
>> Hi, Karl,
>>
>> I'm confused. This should work.
>>
>> while($rows = mysql_fetch_array($result, $rtype)){
>>
>> $rows will be a single dimensional array with indexes ['foo'] and
>> ['bar'].
>>
>> $output[] = $rows;
>> }
>> And $output will be an array of the above array with indexes zero to
>> (number_of_rows_returned - 1).
>>
>> Are you sure this is the exact code? If so, can you show us where you
>> call it and dump the results? Maybe the problem is back there.
>>
>
> I apologize to everyone for the confusion in my original post.
> My var_dump example was wrong.
> I'm getting a multidimensional array and want a single dimension.
>
> I recognize that my use of $output[] = $rows; seems to be the reason why
> I get the multidimensional array. I guess my real issue is figuring out
> how to get ALL returned rows of the single dimensional array. Everything
> else I've tried gives me one row.
>
>
Karl,
I guess I don't understand exactly what you want.
You're returning two columns from your SELECT statement - 'foo' and
'bar'. To place them in one variable you need an array.
Now - you're returning multiple instances of that first array (multiple
rows), so you need a multidimensional array to retrieve all the data.
Maybe what you want is
array(2) {
["foo"]=>array(3) {
[0]=>string(9) "foo Stuff"
[1]=>string(14) "more foo stuff"
[2]=>string(20) "still more foo stuff"
}
["bar"]=>array(3) {
[0]string(9) "bar Stuff"
[1]=>string(14) "more bar stuff"
[2]=>string(20) "still more bar stuff"
}
}
Which is still a multidimensional array, but rotated.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|