Home > Archive > PHP SQL > February 2006 > How can i display a list of mysql databases in php?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How can i display a list of mysql databases in php?
|
|
| Ninja_Monkey 2006-02-19, 6:58 pm |
| 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?
| |
|
|
"Ninja_Monkey" <monkey@home.com> 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/functi...ql-list-dbs.php
| |
| Ninja_Monkey 2006-02-21, 6:58 pm |
| i tried that. but im using mysqli not mysql. that command doesnt exist in
mysqli and ig et a function undefined error.
"Nick" <nick@phpcontrol.co.uk> wrote in message
news:dtd63p$hht$1@news.freedom2surf.net...
>
> "Ninja_Monkey" <monkey@home.com> wrote in message
> news:U13Kf.32192$Fy4.25979@newsfe4-win.ntli.net...
"SHOW[color=darkred]
>
> http://www.php.net/manual/en/functi...ql-list-dbs.php
>
>
| |
|
| 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
| |
| Ninja_Monkey 2006-02-21, 6:58 pm |
| 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] . "<br>\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.
| |
|
| 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] . "<br>\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], "<br>\r\n";
}
mysqli_free_result($result);
mysqli_close($connection);
Cheers,
NC
| |
|
| Ninja_Monkey wrote:
>
> Could you explain how this works for me though?
>
> while ($record = mysqli_fetch_array($result, MYSQLI_NUM)) {
> echo $record[0], "<br>\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
| |
| Ninja_Monkey 2006-02-28, 7:56 am |
| thanks a lot. i understand how it works now.
"NC" <nc@iname.com> wrote in message
news:1141024957.853520.134370@t39g2000cwt.googlegroups.com...
> Ninja_Monkey wrote:
>
> 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
>
|
|
|
|
|