Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 10 Replies) mysql_list_dbs() (Read 4702 times)
rehlbe
God Member
*****
Offline


Juve Merda

Posts: 614
Joined: Feb 1st, 2002
Gender: Male
mysql_list_dbs()
Apr 10th, 2002 at 7:32pm
Print Post  
I need to create a script that prints all mysql databases fo a certain connection: I've tried to use mysql_list_dbs()  ,  but I don't know how to use it..can any1 explain me how this function works? (or even if ya have a diff solution)
  

www.rehlbe.com
Current task -> rewriting website.
Per un aiuto in italiano contattami tramite il mio forum!

Installed Mods = 82
Milan forever
Back to top
IP Logged
 
Administrator
Forum Administrator
*****
Offline


Yummm

Posts: 7
Location: Modders Rile
Joined: Oct 7th, 2014
Gender: Male
Re: mysql_list_dbs()
Reply #1 - Apr 10th, 2002 at 8:36pm
Print Post  
I know this is not very creative but try this:

Code
Select All
<?php
$link = mysql_connect('localhost', 'myname', 'secret');
$db_list = mysql_list_dbs($link);

while ($row = mysql_fetch_object($db_list)) {
    echo $row->Database . "\n";
}
?> 



this is the example code from php.net

Also check http://www.php.net/manual/en/function.mysql-list-dbs.php for usefull comments on that function.
  

The Administrator.
Back to top
WWW  
IP Logged
 
rehlbe
God Member
*****
Offline


Juve Merda

Posts: 614
Joined: Feb 1st, 2002
Gender: Male
Re: mysql_list_dbs()
Reply #2 - Apr 10th, 2002 at 8:48pm
Print Post  
yeah, that was the firstthin I checked out..

anyway this is what I want to do: create a script that prints all data from all tables from every database of a mysql connection --> this is what I've done..any suggestion is welcome!!

Code
Select All
<?php


$host = "localhost";		/*		Host mysql		*/
$uname = "";		  /*	     Nome  Utente	     */
$password = "";	     /*		 Password		 */

$conn = mysql_connect($host, $uname, $password) or die("can't connect to server");
$db_list = mysql_list_dbs($conn) or die ( mysql_error() );

foreach ($db as $db_list)
	{
	mysql_select_db($db) or die ( mysql_error() );
	$tables = mysql_list_tables($db) or die ( mysql_error() );
	while ($table = mysql_fetch_array($tables))
		{
		$query = ("select * from $table");
		$dbtables = mysql_query($query) or die ( mysql_error() );
		$tablenames = mysql_list_fields ($database, $table) or die ( mysql_error() );

		echo "<table border=\"2\">"
		    ."<tr>";
		$i=0;
		while($i <mysql_num_fields($tablenames))
			{
			echo "<td>" .mysql_field_name ($tablenames, $i). "</td>";
			$i++;
			}
		echo "</tr>";

		echo "<tr>";
		while ($row = mysql_fetch_array($tables, MYSQL_ASSOC))
			{
			echo "<tr>";
			while ( list ($key, $value) = each($row) )
				{
				echo "<td>" .$value. "</td>";
				}
			echo "</tr>";
			}
		echo "</table>";
		}
	}

?>
 



but I get this error Quote:
Warning: Invalid argument supplied for foreach() in c:\programmi\easyphp\www\print all.php on line 11
  

www.rehlbe.com
Current task -> rewriting website.
Per un aiuto in italiano contattami tramite il mio forum!

Installed Mods = 82
Milan forever
Back to top
IP Logged
 
Administrator
Forum Administrator
*****
Offline


Yummm

Posts: 7
Location: Modders Rile
Joined: Oct 7th, 2014
Gender: Male
Re: mysql_list_dbs()
Reply #3 - Apr 10th, 2002 at 8:56pm
Print Post  
replace

Code
Select All
foreach ($db as $db_list)
 { 


with
Code
Select All
while ($row = mysql_fetch_object($db_list)) {
    $db = $row->Database; 


  

The Administrator.
Back to top
WWW  
IP Logged
 
rehlbe
God Member
*****
Offline


Juve Merda

Posts: 614
Joined: Feb 1st, 2002
Gender: Male
Re: mysql_list_dbs()
Reply #4 - Apr 10th, 2002 at 9:00pm
Print Post  
Cry Cry I get Quote:
Table 'mysql.array' doesn't exist


  

www.rehlbe.com
Current task -> rewriting website.
Per un aiuto in italiano contattami tramite il mio forum!

Installed Mods = 82
Milan forever
Back to top
IP Logged
 
rehlbe
God Member
*****
Offline


Juve Merda

Posts: 614
Joined: Feb 1st, 2002
Gender: Male
Re: mysql_list_dbs()
Reply #5 - Apr 11th, 2002 at 8:45am
Print Post  
I're restarted from beginning..so I first created a script that shows all content of a table, then 1 to show all tables in a database: the last thing I need is a script that shows all databases for a connection..this is what I have:

Code
Select All
<?php

$host = "localhost";      /*            Host mysql            */        
$uname = "";              /*           Nome  Utente           */  
$password = "";           /*             Password             */

$conn = mysql_connect($host, $uname, $password) or die("can't connect to server");

$alldatabases = mysql_list_dbs($conn) or die ( mysql_error() );
for($d=0; $d<mysql_num_rows($alldatabases); $d++)
      {

      $db = mysql_select_db($database);
      $alltables = mysql_list_tables($database) or die ( mysql_error() );
      for($a=0; $a<mysql_num_rows($alltables); $a++)
            {
                  $table = mysql_tablename($alltables, $a);
                  $query = ("select * from $table");
                  $tables = mysql_query($query) or die ( mysql_error() );
                  $tablenames = mysql_list_fields ($database, $table) or die ( mysql_error() );

                  echo "<center>"
                      ."<table border=\"2\" width=\"100%\">"
                      ."<tr>"
                      ."<td width=\"100%\"><b><font size=\"+1\"><center>" .$table. "</center></font></b></td>"
                      ."</tr>"
                      ."<tr>"
                      ."<table border=\"1\" width=\"100%\">"
                      ."<tr>";
                  $i=0;
                  while($i <mysql_num_fields($tablenames))
                        {
                        echo "<td><b>" .mysql_field_name ($tablenames, $i). "</b></td>";
                        $i++;
                        }
                  echo "</tr>";
                  echo "<tr>";
                  while ($row = mysql_fetch_array($tables, MYSQL_ASSOC))
                        {
                        echo "<tr>";
                        while ( list ($key, $value) = each($row) )
                              {
                              echo "<td>" .$value. "</td>";
                              }
                        echo "</tr>";
                        }
                  echo "</table>";
            }
      }
?>
 



but obviously this
Code
Select All
for($d=0; $d<mysql_num_rows($alldatabases); $d++) 

doesn't work for databases..what should I do??
  

www.rehlbe.com
Current task -> rewriting website.
Per un aiuto in italiano contattami tramite il mio forum!

Installed Mods = 82
Milan forever
Back to top
IP Logged
 
Administrator
Forum Administrator
*****
Offline


Yummm

Posts: 7
Location: Modders Rile
Joined: Oct 7th, 2014
Gender: Male
Re: mysql_list_dbs()
Reply #6 - Apr 11th, 2002 at 12:09pm
Print Post  
why don't you use the code I sugggested above?

Code
Select All
while ($row = mysql_fetch_object($db_list)) {
    $db = $row->Database; 

  

The Administrator.
Back to top
WWW  
IP Logged
 
rehlbe
God Member
*****
Offline


Juve Merda

Posts: 614
Joined: Feb 1st, 2002
Gender: Male
Re: mysql_list_dbs()
Reply #7 - Apr 11th, 2002 at 12:33pm
Print Post  
coz it gives me Quote:
No Database Selected
Undecided
  

www.rehlbe.com
Current task -> rewriting website.
Per un aiuto in italiano contattami tramite il mio forum!

Installed Mods = 82
Milan forever
Back to top
IP Logged
 
rehlbe
God Member
*****
Offline


Juve Merda

Posts: 614
Joined: Feb 1st, 2002
Gender: Male
Re: mysql_list_dbs()
Reply #8 - Apr 11th, 2002 at 3:47pm
Print Post  
I'm really hating this thing...I've tried to use mysql_db_name() now, wich seems to be similar to list_dbs;
I've found a ?funny? thing..look at this
Code
Select All
<?php
$host = "localhost";      /*            Host mysql            */
$uname = "";              /*           Nome  Utente           */
$password = "";           /*             Password             */

$connection = mysql_connect("$host", "$uname", "$password") or die("can't connect to server");
$db_list = mysql_list_dbs();
$d = 0;
$cnt = mysql_num_rows($db_list);
while ($d < $cnt)
      {
              $db = mysql_db_name($db_list, $d);
            echo $db;                      /* <---why does it print the variable, but not use it as a database name for the line below??? */
            mysql_select_db($db) or die ( mysql_error() );
            $alltables = mysql_list_tables($db) or die ( mysql_error() );
            for($a=0; $a<mysql_num_rows($alltables); $a++)
                  {
                        $table = mysql_tablename($alltables, $a);
                        $query = ("select * from $table");
                        $tables = mysql_query($query) or die ( mysql_error() );
                        $tablenames = mysql_list_fields ($database, $table) or die ( mysql_error() );

                        echo "<center>"
                            ."<table border=\"2\" width=\"100%\">"
                            ."<tr>"
                            ."<td width=\"100%\"><b><font size=\"+1\"><center>" .$table. "</center></font></b></td>"
                            ."</tr>"
                            ."<tr>"
                            ."<table border=\"1\" width=\"100%\">"
                            ."<tr>";
                        $i=0;
                        while($i <mysql_num_fields($tablenames))
                              {
                              echo "<td><b>" .mysql_field_name ($tablenames, $i). "</b></td>";
                              $i++;
                              }
                        echo "</tr>";
                        echo "<tr>";
                        while ($row = mysql_fetch_array($tables, MYSQL_ASSOC))
                              {
                              echo "<tr>";
                              while ( list ($key, $value) = each($row) )
                                    {
                                    echo "<td>" .$value. "</td>";
                                    }
                              echo "</tr>";
                              }
                        echo "</table>";
                  }      
              $d++;      
          }
?>
 



If you try to use it it'll not work cause it'll say "no database selected"...but if u try to print the variable $db the database name will come out!!!

any ideas ?  Cry
  

www.rehlbe.com
Current task -> rewriting website.
Per un aiuto in italiano contattami tramite il mio forum!

Installed Mods = 82
Milan forever
Back to top
IP Logged
 
rehlbe
God Member
*****
Offline


Juve Merda

Posts: 614
Joined: Feb 1st, 2002
Gender: Male
Re: mysql_list_dbs()
Reply #9 - Apr 11th, 2002 at 3:57pm
Print Post  
wtf..now it works: I must be a really bumb man if I posted here 7 times just because...I forgot to change a variable!!!!!!!!!!!    :mad: :mad: :mad: :mad: :mad: :mad: :mad:

The problem was this:
Code
Select All
$tablenames = mysql_list_fields ($database, $table) or die ( mysql_error() );  


instead of
Code
Select All
$tablenames = mysql_list_fields ($db, $table) or die ( mysql_error() );  



Embarrassed Embarrassed
  

www.rehlbe.com
Current task -> rewriting website.
Per un aiuto in italiano contattami tramite il mio forum!

Installed Mods = 82
Milan forever
Back to top
IP Logged
 
Administrator
Forum Administrator
*****
Offline


Yummm

Posts: 7
Location: Modders Rile
Joined: Oct 7th, 2014
Gender: Male
Re: mysql_list_dbs()
Reply #10 - Apr 11th, 2002 at 6:44pm
Print Post  
well * Administrator is glad that it works now Grin
  

The Administrator.
Back to top
WWW  
IP Logged
 
DemonSlayer
God Member
*****
Offline


I am the face of pure
evil!

Posts: 1398
Joined: Jul 26th, 2001
Gender: Male
Re: mysql_list_dbs()
Reply #11 - Apr 12th, 2002 at 2:09am
Print Post  
Belive me rehble that is very common!
  
Back to top
ICQAIM  
IP Logged
 
rehlbe
God Member
*****
Offline


Juve Merda

Posts: 614
Joined: Feb 1st, 2002
Gender: Male
Re: mysql_list_dbs()
Reply #12 - Apr 12th, 2002 at 8:33am
Print Post  
hehe..unfortunately I know that  Undecided Undecided

I think that my pc is a sort of executioner  Grin
  

www.rehlbe.com
Current task -> rewriting website.
Per un aiuto in italiano contattami tramite il mio forum!

Installed Mods = 82
Milan forever
Back to top
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint