Jump to content

Printing out MySQL table names with PHP


rubyarat2010
Go to solution Solved by JIXO,

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
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);

Link to comment
Share on other sites

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);
Edited by rubyarat2010
Link to comment
Share on other sites

  • Solution

<?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);
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.