Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function for putting all database table to an array

Tags:

php

mysql

I have written a function to print database table to an array like this

$db_array=  
 Array(
    ID=>1,
    PARENTID =>1,
    TITLE => LIPSUM,
    TEXT =>LIPSUM
    )

My function is:

function dbToArray($table)
                        {
                          $allArrays =array();
                          $query = mysql_query("SELECT * FROM $table");
                          $dbRow = mysql_fetch_array($query);
                            for ($i=0; $i<count($dbRow)  ; $i++)
                                {
                                   $allArrays[$i] =  $dbRow; 
                                }
                            $txt .='<pre>';
                            $txt .= print_r($allArrays);
                            $txt .= '</pre>';
                            return $txt;
                        }

Anything wrong in my function. Any help is appreciated about my problem. Thanks in advance


1 Answers

Seems like you are trying to print a table (not a database)

function printTable($table){
   $allRows = array();
   $query = mysql_query("SELECT * FROM $table");
   while($row = mysql_fetch_array($query)){
      $allArrays[] =  $row; 
   }
   $txt ='<pre>';
   $txt .= print_r($allRows, true);
   $txt .= '</pre>';

   echo $txt;

   return $txt;
}

(Edit: fixed missing second parameter in print_r)

like image 93
Vidar Vestnes Avatar answered Apr 10 '26 22:04

Vidar Vestnes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!