PHP: Getting List of Database & Tables in MySQL



PHP supports Object oriented Concepts hence one Class is created named dbconnect which has reusable database  functions like selecting database, firing query and getting result etc

Query1: Show Databases  returns List  of Databses in MySQl
Query2: Show Tables  returns List  of Tables in selected Database.


dbconnect.php
<?php
Class dbconnect
 {
  function __Construct()
   {
     $this->cn=mysql_connect("localhost","root");
    
   }
function firequery($q)
   {
    $res=mysql_query($q);
    return $res;
   }


 function __Destruct()
   {
     mysql_close( $this->cn);
    
   }
function selectdb($name)
   {
    mysql_select_db($name);

   }
  
  }

?>

ListDatabase.php


<?php
try
{
require_once('dbconnect.php');//to make available database functions from dbconnect.php
$obj=new dbconnect();

$res=$obj->firequery('Show Databases');
echo '<h3>List of Databases in MySQL</h3>';
echo '<form name="frm" method="post"><select name="s1" multiple onchange=submit();>';
while($row=mysql_fetch_row($res))
 {
  echo "<option>$row[0]</option>";
  }
echo '</select></form>';


if(isset($_POST['s1']))
 {
  $obj->selectdb($_POST['s1']);
  $res=$obj->firequery('show tables');
echo "<h3>Tables in <i>".$_POST['s1']. "</i></h3>";
while($row=mysql_fetch_row($res))
 {
  echo "$row[0]<br/>";
  }
 }

}

catch(Exception $e)
  {
   echo 'OOPS!'.$e->getMessage();
  }

?>

1 comment:

  1. I loved as much as you will receive carried out right here. The sketch is attractive, your authored subject matter stylish. nonetheless, you command get got an shakiness over that you wish be delivering the following. unwell unquestionably come more formerly again as exactly the same nearly very often inside case you shield this increase.
    Remove Pending Friend Request Facebook : Increase chance of not getting block

    ReplyDelete

Thanks for visiting my Blog!

Have a Question ? Need Help in College Assignments, Need Code that is not available here? Just leave a comment & get your code instantly.

Tips to Enhance Your Blog