How can i display a list of mysql databases in php?

How can i display a list of mysql databases in php?

am 19.02.2006 19:46:12 von Ninja_Monkey

im trying to write a page that can display a list of the databases from
mysql. i cant seem to find any commands for it other than executing "SHOW
DATABASES" as an SQL command. what am i doing wrong?

Re: How can i display a list of mysql databases in php?

am 20.02.2006 20:42:51 von Nick

"Ninja_Monkey" wrote in message
news:U13Kf.32192$Fy4.25979@newsfe4-win.ntli.net...
> im trying to write a page that can display a list of the databases from
> mysql. i cant seem to find any commands for it other than executing "SHOW
> DATABASES" as an SQL command. what am i doing wrong?
>
>

http://www.php.net/manual/en/function.mysql-list-dbs.php

Re: How can i display a list of mysql databases in php?

am 21.02.2006 19:04:02 von Ninja_Monkey

i tried that. but im using mysqli not mysql. that command doesnt exist in
mysqli and ig et a function undefined error.

"Nick" wrote in message
news:dtd63p$hht$1@news.freedom2surf.net...
>
> "Ninja_Monkey" wrote in message
> news:U13Kf.32192$Fy4.25979@newsfe4-win.ntli.net...
> > im trying to write a page that can display a list of the databases from
> > mysql. i cant seem to find any commands for it other than executing
"SHOW
> > DATABASES" as an SQL command. what am i doing wrong?
> >
> >
>
> http://www.php.net/manual/en/function.mysql-list-dbs.php
>
>

Re: How can i display a list of mysql databases in php?

am 21.02.2006 19:38:02 von nc

Ninja_Monkey wrote:
>
> im trying to write a page that can display a list of the databases from
> mysql. i cant seem to find any commands for it other than executing
> "SHOW DATABASES" as an SQL command. what am i doing wrong?

Nothing. Executing a "SHOW DATABASES" query is what you should do.

Cheers,
NC

Re: How can i display a list of mysql databases in php?

am 21.02.2006 21:04:00 von Ninja_Monkey

ill show you the code im trying.

$connection = mysqli_connect($_SESSION['host'], $_SESSION['user'],
$_SESSION['pass']);
$sql = "SHOW DATABASES";
$result = mysqli_query($connection, $sql);
$dblist = mysqli_fetch_array($result, MYSQLI_NUM); # A PRINT OF THIS
SAYS Array
$c = 0;
while (count($dblist) > $c) {
print_r($dblist[$c] . "
\n"); #THIS PRINTS OUT AS:
"information schema". IT ONLY PRINTS 1 aswell.
$c++;
}
$result = NULL;
@mysqli_free_result($result);
mysqli_close($connection);

What is an information Schema??? and why does this not work.

Re: How can i display a list of mysql databases in php?

am 22.02.2006 20:35:36 von nc

Ninja_Monkey wrote:
> ill show you the code im trying.
>
> $connection = mysqli_connect($_SESSION['host'], $_SESSION['user'],
> $_SESSION['pass']);
> $sql = "SHOW DATABASES";
> $result = mysqli_query($connection, $sql);
> $dblist = mysqli_fetch_array($result, MYSQLI_NUM); # A PRINT OF THIS
> SAYS Array
> $c = 0;
> while (count($dblist) > $c) {
> print_r($dblist[$c] . "
\n"); #THIS PRINTS OUT AS:
> "information schema". IT ONLY PRINTS 1 aswell.
> $c++;
> }
> $result = NULL;
> @mysqli_free_result($result);
> mysqli_close($connection);
>
> What is an information Schema??? and why does this not work.

Basically, because you think mysqli_fetch_array() returns a list of
databases (it doesn't). A SHOW DATABASES query returns a list of
databases, with each name in its own record. So you need to read
through the result set; something like this:

$connection = mysqli_connect($_SESSION['host'],
$_SESSION['user'], $_SESSION['pass']);
$sql = "SHOW DATABASES";
$result = mysqli_query($connection, $sql);
while ($record = mysqli_fetch_array($result, MYSQLI_NUM)) {
echo $record[0], "
\r\n";
}
mysqli_free_result($result);
mysqli_close($connection);

Cheers,
NC

Re: How can i display a list of mysql databases in php?

am 26.02.2006 19:08:10 von Ninja_Monkey

Thanks i used that and it worked. Thanks very much.

Could you explain how this works for me though?

while ($record = mysqli_fetch_array($result, MYSQLI_NUM)) {
echo $record[0], "
\r\n";
}

does this style of while loop simply count through the array? im just not
sure how the syntax works?


"NC" wrote in message
news:1140636936.099814.67820@z14g2000cwz.googlegroups.com...
> Ninja_Monkey wrote:
> > ill show you the code im trying.
> >
> > $connection = mysqli_connect($_SESSION['host'], $_SESSION['user'],
> > $_SESSION['pass']);
> > $sql = "SHOW DATABASES";
> > $result = mysqli_query($connection, $sql);
> > $dblist = mysqli_fetch_array($result, MYSQLI_NUM); # A PRINT OF
THIS
> > SAYS Array
> > $c = 0;
> > while (count($dblist) > $c) {
> > print_r($dblist[$c] . "
\n"); #THIS PRINTS OUT AS:
> > "information schema". IT ONLY PRINTS 1 aswell.
> > $c++;
> > }
> > $result = NULL;
> > @mysqli_free_result($result);
> > mysqli_close($connection);
> >
> > What is an information Schema??? and why does this not work.
>
> Basically, because you think mysqli_fetch_array() returns a list of
> databases (it doesn't). A SHOW DATABASES query returns a list of
> databases, with each name in its own record. So you need to read
> through the result set; something like this:
>
> $connection = mysqli_connect($_SESSION['host'],
> $_SESSION['user'], $_SESSION['pass']);
> $sql = "SHOW DATABASES";
> $result = mysqli_query($connection, $sql);
> while ($record = mysqli_fetch_array($result, MYSQLI_NUM)) {
> echo $record[0], "
\r\n";
> }
> mysqli_free_result($result);
> mysqli_close($connection);
>
> Cheers,
> NC
>

Re: How can i display a list of mysql databases in php?

am 27.02.2006 08:22:37 von nc

Ninja_Monkey wrote:
>
> Could you explain how this works for me though?
>
> while ($record = mysqli_fetch_array($result, MYSQLI_NUM)) {
> echo $record[0], "
\r\n";
> }

When you pass a SELECT query to mysqli_query() function, it returns a
pointer to a result set, conceptually similar to a file pointer
returned by fopen(). In both cases, you need to use the pointer to
read the data it points to, record by record (or, in case of a file,
line by line). Each time you call mysqli_fetch_array(), it reads
another record from the result set until it reaches the end of the
result set. A record is returned as an array, whose structure
replicates that requested by query. In your case, the query was SHOW
DATABASES, and each record in a result set has only one field, which by
definition gets number 0...

Cheers,
NC

Re: How can i display a list of mysql databases in php?

am 28.02.2006 12:57:54 von Ninja_Monkey

thanks a lot. i understand how it works now.


"NC" wrote in message
news:1141024957.853520.134370@t39g2000cwt.googlegroups.com.. .
> Ninja_Monkey wrote:
> >
> > Could you explain how this works for me though?
> >
> > while ($record = mysqli_fetch_array($result, MYSQLI_NUM)) {
> > echo $record[0], "
\r\n";
> > }
>
> When you pass a SELECT query to mysqli_query() function, it returns a
> pointer to a result set, conceptually similar to a file pointer
> returned by fopen(). In both cases, you need to use the pointer to
> read the data it points to, record by record (or, in case of a file,
> line by line). Each time you call mysqli_fetch_array(), it reads
> another record from the result set until it reaches the end of the
> result set. A record is returned as an array, whose structure
> replicates that requested by query. In your case, the query was SHOW
> DATABASES, and each record in a result set has only one field, which by
> definition gets number 0...
>
> Cheers,
> NC
>