Re: PHP accessing MySQL

From: Date: Sat, 19 May 2001 01:21:55 +0000
Subject: Re: PHP accessing MySQL
Groups: php.db 
Request: Send a blank email to php-db+get-9459@lists.php.net to get a copy of this message
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!
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.
<? $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 ];
} 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.
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.
Thank you very much for your help, Jonathan
My pleasure - you're digging on it too. Miles
Miles Thompson <milesthompson@ns.sympatico.ca> said: Well, when you debug it, what do you get? What's the value of $dbs? Did you try the simple example shown in the manual, using your information. Did it work? I don't see where you are incrementing $db_num, that could be where your error is coming from. Also check to see if what you are getting back from mysql_tablename() is text or and index. Good luck - Miles Thompson At 01:33 PM 5/18/01 -0600, you wrote:
I am making a script to access MySQL and when I run it I get this: Warning: 0 is not a MySQL link index in
/sites/htdocs/php/db_listtables.php
on line 10 Following is the first 10 lines of my script: <? $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."); Any Ideas? Thank you in advance!! Jonathan -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net For additional commands, e-mail: php-db-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net
-- Jonathan Duncan nacnud.com administrator Nacnud, Inc. ________________________________________________________________ Brought to you by nacnudMail using TWIG. http://www.nacnud.com


« previous php.db (#9459) next »