PHP / SQL Question: Returning array in function

Community Forums/General Help/PHP / SQL Question: Returning array in function

Guy Fawkes(Posted 2013) [#1]
Hello, everyone! =) I have a quick PHP question. :)

My question is:

    Why is this not letting me return all the variables in the entire database of the database selected, like it should?

    This should basically return an array that holds all the data in the selected database, in order from 1st to last & field by field.

    I need to be able to do a print_r() and have it print_r() everything in this array, not just the last values in the database. (id: 5 in this case) as I have 5 entries in the database all with 32 fields. It should start with: (id: 1).

    I should be able to do a print_r() outside of the function, and return the same results as I had when I did a print_r() inside the for loop of the function.

    Anyway, here is the function, I made it as short as possible, as I know you are all limited on time.

Thank You ALL! I REEEEEEEEEEEEEEALLY appreciate this! =)

Here's the link:

    http://codepad.org/erkeeRIe

Once again, thank you guys so much! =)


Thundros~


Derron(Posted 2013) [#2]
1. you do not fetch all data from a selected database - you just fetch data from a selected table (a db is holding multiple tables) - if you really want to fetch all, you would first have to fetch all table-data, then for each table, the table's data.

2. you fetch everything from a table (-> all fields) and then assign each field to an array instead of just assigning the whole result-array to your result.

You:
          while($row = mysql_fetch_array($result))
            {
                for($index = 0; $index <= $numOfCols-1; $index++)
                {
                    $data[$index] = $row[$index];
                }

	    }


is the same as:
          while($row = mysql_fetch_array($result))
                    $data[] = $row;


instead of $data[] (which means, just assign the right side to a new slot within the array) you could also do someting like:
$data[ $row[$primaryKey] ] = $row
This would make an associative array with "keys" being the ids of the row. Only possible if the primary key is something "useful" (no concat of multiple fields). You would need the knowledge of the primary key ... if you want to automate it, use the query:
SHOW KEYS FROM table WHERE Key_name = 'PRIMARY'

Remember: it may return more than one key (eg. for tables having no unique key like "ID" but uniques like "ip_adress" connected to "datetime").
For such cases: just concat the $primaryKey each "while"-loop (store the primary key fields in an array and access $row[$keyfield] to generate a unique key).


bye Ron

PS: hope you do not mind to respond this time to my answer - I do not like to help people which drop in every 1-2 weeks asking for help and forgetting to say whether it helped or not.



edit:
 $finalarray = array();
    $finalarray = GetData($get_user_details);

There is no need to predefine $finalarray, the "GetData"-function-call is defining it too. Save that line.
The only need for predefinition is something like:

 $myvar = array();
 if( $anotherVar ) { $myvar = somethingdifferent(); }
 print_r( $myvar );

Without predefinition the print_r may trigger an exception as soon as "anotherVar" isn't true.


edit2:
You are using:
       $data = array();
...
	$data[] = array();

What it does is:
a) defining $data as an array
b) defining a new entry within $data as an array.
so after "b)" $data is array(0=>array()).

Just remove "b)".