Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP get mysql table info function
    text
    copied!<p>I am making a PHP function to return an array of a mysql database's table's info. I am new to coding in php and am looking for more efficient way to do the same thing I have done because my method does not seem very efficient. Would this be a good place to use a mysql join statement?</p> <p>Here is my function.</p> <pre><code>public static function getAllTables() { // Get an array of all the tables in the database $sql = "SHOW TABLES"; //this makes a connection to mysql database using mysqli $mysqlConnection = new CMySqlConnection(); $result = $mysqlConnection-&gt;mysqli-&gt;query($sql); $tablesArray = array(); while($row = $result-&gt;fetch_row()) { $tablesArray[$row[0]]=array(); } $result-&gt;close(); //Get an array of all the table names in database $arrayKeys = array_keys($tablesArray); //foreach table get the column's info foreach($arrayKeys as $key){ $sql=" SHOW COLUMNS from " . $key; $result = $mysqlConnection-&gt;mysqli-&gt;query($sql); for($i = 0; $row = $result-&gt;fetch_row(); $i++) { //format the array to use a descriptive key rather than a number $row['columnName'] = $row[0]; $row['type'] = $row[1]; $row['null'] = $row[2]; $row['key'] = $row[3]; $row['default'] = $row[4]; $row['extra'] = $row[5]; unset($row[0]); unset($row[1]); unset($row[2]); unset($row[3]); unset($row[4]); unset($row[5]); // put the column info into the tables array $tablesArray[$key][$i] = $row; } $result-&gt;close(); } $mysqlConnection-&gt;Disconnect(); // return the tables array return $tablesArray; } </code></pre> <p>Thanks for any input :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload