Page 1 of 1

Putting query results into a drop down box....

Posted: Fri Mar 12, 2004 9:55 am
by struggling_student
hi

I need to run this query:

select distinct db from mysql.db where select_priv = 'y' and user = 'jblogs';

and take the database names and put them in a drop down box so that when a database name is selected, the table names are displayed underneath it. Any ideas?

Posted: Mon Aug 30, 2004 8:41 am
by Lord Sauron
Yep, I do have ideas ;-)
Although it would help if I would understand your DB-structure. You're using strange names. 'db' is the column containing the table-names? And mysql.db is a table containing the information of all databases?

Something like this (guess it is not completely error-free)

Code: Select all

<?php


$result = mysql_query("SELECT distinct db FROM mysql.db where select_priv = 'y' and user = 'jblogs';",mySQLConnection) OR die("wrong query");
 
$databNames = mysql_fetch_row($result);

IF (empty($databNames)) {
    print("No databases available\n");
} // Close if

ELSE {
    // Create form
    print("<FORM name="displayTableNames" method='POST' action="displayNames.php?dbNames=$dbNames">\n\n");					
    print("<SELECT CLASS="selectBox" NAME='dbNames' TABINDEX="1" SIZE="2">\n"); 
						
    while(!empty($databNames)) {
        print("<OPTION VALUE='$name' SELECTED>".$databNames[0]."</OPTION>\n");
        $databNames = mysql_fetch_assoc($result);
    } // Close while loop

    print("</SELECT>\n");

    print("<INPUT class="button" type="submit" value="Show names" name="showNamesButton" tabindex="3">\n");							
    print("</FORM>\n");


} // Close else

?>

Code: Select all

<?php
// displayNames.php

    IF (!empty($dbNames)) {
        // Query for searching the table names of this database
        $tableNames = "SELECT tableNames FROM Database WHERE Database=".$dbNames.";"

        ECHO $dbNames;
        ECHO $row = mysql_fetch_row($tableNames)
        ECHO "<TABLE">
            ECHO "<TR>";
                while (!empty($row))) {
                    ECHO "<TD>";                
                    ECHO $row."<BR>";
                    $row = mysql_fetch_row($tableNames);
                    ECHO "</TD>";                
                }
            ECHO "</TR>";
        ECHO "</TABLE">
    }

?>

Posted: Mon Aug 30, 2004 10:06 am
by feyd

Code: Select all

SHOW DATABASES

Posted: Mon Aug 30, 2004 11:34 am
by Lord Sauron
Ah, that's what he means ;-)

True, that would be the right sql-query.