Jump to content

Printing out MySQL table names with PHP


rubyarat2010

Recommended Posts

So I want to get each table name in a MYSQL database and put it in the variable database.  This is what I have so far but I cant seam to get it working, thanks in advance!  It just says "Access denied for user ''@'localhost' to database 'mynotesdatabase'" But when I print out the actual rows of the tables it works but not when I want to print out the table names.

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  else{
  echo "Connected", "<br/>";
  }
  
  
  
  
   $query = mysql_query("SHOW TABLES FROM mynotesdatabase") or die(mysql_error());



 while($row = mysql_fetch_array($query)) {

$database = $query;
 
$result = mysqli_query($con,"SELECT * FROM $database");
echo "<b>+" . $database . "</b><br/>";
while($row = mysqli_fetch_array($result))
  {
  $type = "";
  if($row['Type'] == 0){
  $type = "Link";
  }
  else{
  $type="Note";
  }
  echo "&nbsp -" . $row['Name'] . " (" . $type . ")";
  echo "<br>";
  }
  
  
 }
 
 


mysqli_close($con);

Link to comment
https://forums.phpfreaks.com/topic/285030-printing-out-mysql-table-names-with-php/
Share on other sites

Hey man thank you so much! I did not see those, so I changed them all to mysql.  Now I have this but all it says is "No database selected":

  
  
  
   $query = mysql_query("SHOW TABLES FROM mynotesdatabase") or die(mysql_error());



 while($row = mysql_fetch_array($query)) {

$database = $query;
 
$result = mysql_query("SELECT * FROM $database", $con) or die(mysql_error());
echo "<b>+" . $database . "</b><br/>";
while($row = mysql_fetch_array(mysql_query($result, $con)))
  {
  $type = "";
  if($row['Type'] == 0){
  $type = "Link";
  }
  else{
  $type="Note";
  }
  echo "&nbsp -" . $row['Name'] . " (" . $type . ")";
  echo "<br>";
  }
  
  
 }
 
 


mysql_close($con);

Also I realized that the variable database is not database but tables.  Just an FYI.  Here is the full code if you want to see it, sorry to double post 

$con=mysql_connect("*", "*", "*", "mynotesdatabase") or die(mysql_error());


  
  
  
  
$query = mysql_query("SHOW TABLES FROM mynotesdatabase") or die(mysql_error());

 while($row = mysql_fetch_array($query)) {

$table = $query;
 
$result = mysql_query("SELECT * FROM $table", $con) or die(mysql_error());
echo "<b>+" . $table . "</b><br/>";
	while($row = mysql_fetch_array(mysql_query($result, $con)))
	{
	$type = "";
		if($row['Type'] == 0){
		$type = "Link";
		}
		else{
		$type="Note";
		}
	echo "&nbsp -" . $row['Name'] . " (" . $type . ")";
	echo "<br>";
	}
  
  
 }
 
 


mysql_close($con);

<?php
$conn = new mysqli('*', '*', '*');
$conn->select_db('mynotesdatabase');
$query = mysqli_query($conn, 'SHOW TABLES') or die(mysql_error());
while($tableShow = mysqli_fetch_row($query)) {
$result = mysqli_query($conn, "SELECT * FROM {$tableShow[0]}") or die(mysql_error());
echo "<b>+" . $tableShow[0] . "</b><br/>";
while($fields = mysqli_fetch_assoc($result)) {
$type = "";
if($fields['Type'] == 0) {
$type = "Link";
}
else {
$type="Note";
}
echo "&nbsp -" . $fields['Name'] . " (" . $type . ")";
echo "<br>";
}
}
mysql_close($conn);

Sorry, the above was messed up, try this :

<?php
$conn = new mysqli('*', '*', '*');
$conn->select_db('mynotesdatabase');
$query = mysqli_query($conn, 'SHOW TABLES') or die(mysql_error());
while($tableShow = mysqli_fetch_row($query)) {
    $result = mysqli_query($conn, "SELECT * FROM {$tableShow[0]}") or die(mysql_error());
    echo "<b>+" . $tableShow[0] . "</b><br/>";
    while($fields = mysqli_fetch_assoc($result)) {
        $type = "";
        if($fields['Type'] == 0) {
            $type = "Link";
        }
        else {
            $type="Note";
        }
        echo "&nbsp -" . $fields['Name'] . " (" . $type . ")";
        echo "<br>";
    }
}
mysql_close($conn);

If you get any error, paste it here.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.