Re: PHP accessing MySQL

From: Date: Tue, 22 May 2001 20:44:39 +0000
Subject: Re: PHP accessing MySQL
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-9540@lists.php.net to get a copy of this message
"Miles Thompson" <milesthompson@ns.sympatico.ca> wrote in message news:5.0.2.1.0.20010518222140.00aed900@pop1.ns.sympatico.ca... > Jonathan .. see below. > At 08:38 PM 5/18/01 +0000, Jonathan Duncan wrote: > >Miles > > > >Thank you for the response. I did some debugging and the value of $dbs is 2. > > > >I am not sure of what example you are referring to. I am not using the > >manual currently, although I have the PDF version of it. > > http://www.php.net/mysql_list_dbs > for on line version, with annotations! > I tried the example found on the site: while ($row = mysql_fetch_object($dbs)) { echo $row->Database . "\n"; } And the results were null. It listed nothing. I have a question about this example, what is the "->" supposed to do? > > >The incrementation of $db_num happens on line 24 but I only listed the first > >10 lines because it seems to be a problem within the first 10 lines as the > >error message indicates. > > > >If I change line 10 from: > > > >$tables = mysql_list_dbs($db_names[$db_num]) > > > >to: > > > >$tables = mysql_list_dbs() > > > >the script runs and gives me a list of the databases redundantly: > > > >-mysql > >-test > > mysql > > test > > twignud > >-twignud > > mysql > > test > > twignud > > Cool! Although it's not what you want. <g>. > But what is it telling us? We're just running through two loops, but really > asking for the same information each time, if I interpret it correctly. > > And if you look at your code, you have two nested loops, doing essentially > the same thing - listing the databases. > That is pretty much what I am trying to do. I want to list each database and then the tables within those databases. > > ><? > > $connection = mysql_connect("localhost", "user", > > "password") or die > >("Couldn't connect."); > > $dbs = mysql_list_dbs($connection) or die("Couldn't list databases."); > > $db_list = "<UL>"; > > $db_num = 0; > > while ($db_num < mysql_num_rows($dbs)) { > > $db_names[$db_num] = mysql_tablename($dbs, $db_num); > > $db_list .= "<LI>$db_names[$db_num]"; > > if (($db_names[$db_num] != "mysql") && ($db_names[$db_num] ! > >= "tempdata")) { > > $tables = mysql_list_dbs($db_names[$db_num]) or die("Couldn't list > >databases."); > > $tables = mysql_list_dbs() or die("Couldn't list databases."); > > $table_list = "<UL>"; > > $table_num = 0; > > while ($table_num < mysql_num_rows($tables)) { > > $table_names[$table_num] = mysql_tablename($tables, $table_num); > > $table_list .= "<LI>$table_names[$table_num]"; > > $table_num++; > > } > > $table_list .= "</UL>"; > > } > > $db_list .= "$table_list"; > > $db_num++; > > } > > $db_list .= "</UL>"; > >?> > > > >This is minus my debugging additions. I tried to get info from > >$mysql_tablename but it came up blank every way I tried it. It is as though > >nothing is getting passed into $mysql_tablename. > > That's strange, because this annotation came from zak@php: > [Editor's Note: mysql_db_name(), mysql_dbname() and mysql_tablename() > are all aliases for mysql_result() and should behave in exactly the same > fashion. --zak@php.net] > > Use mysql_db_name() instead of mysql_tablename() to read databases. > http://php.net/manual/en/function.mysql-db-name.php > > So this should give us a list of keys and databases: > while( $row = mysql_fetch_array( $dbs ) ) > { > echo $row[ 0 ]; > echo $row[ 1 ]; > } > while($row = mysql_fetch_array($dbs)) { echo "Row 0 is ", $row[0]; echo "Row 1 is ", $row[1]; } Returns: Row 0 is mysqlRow 1 is Row 0 is testRow 1 is Row 0 is twignudRow 1 is -IE- Row 0 is mysql Row 1 is Row 0 is test Row 1 is Row 0 is twignud Row 1 is > I'm wondering, if $row[ 0 ] returns the name of the database, and $row[ 1 ] > returns "array". (Or vice versa.) If so, we should be able to nest another > while () in there and fetch the table names. (If that's your intention.) > > I'll warn you that I get lost in multi-dimensional arrays very quickly, and > I'm freelancing this off the top of my head. > It seems that Row 1 doesn't have anything which may be the source of the problem. What do you think of this? > >Does this clarify anything in your mind? I am taking this script straight > >from a book. However, the book is designed for PHP 4 and I am using PHP > >3.0.16, although I don't know if that would make a difference. > > Shouldn't, but is there any reason why you can't go to 4.0.5? Why not get > the database names loop working first, and then go for the table names, a > divide and conquer approach. > Sounds like a good plan, I will see what I can do. > >Thank you very much for your help, > >Jonathan > > My pleasure - you're digging on it too. > Miles > Yep, I love this stuff!

« previous php.db (#9540) next »