0

How do i loop only the database names that starts with solarsystem inside a dropdown list.

+---------------------+
|       dbname        |
+---------------------+
| electronics         |
| vegetables          |
| solarsystem_sun     |
| solarsystem_mercury |
| solarsystem_venus   |
| solarsystem_mars    |
| fruits              |
| foobar              |
+---------------------+

Currently it is looping all the database names

<div class="select">
    <?php
        $sdbhost = 'localhost';
        $sdbusername = 'root';
        $sdbpassword = '';
        $dbcnx = mysql_connect ($sdbhost, $sdbusername, $sdbpassword);

        $dbresult = mysql_query("SHOW DATABASES");
            echo "<select name='dbselect'>";
                while ($dbrow = mysql_fetch_array($dbresult)) {
                    echo "<option input value='$dbrow[0]'>" .$dbrow[0]."<br></option>";
                }
            echo "<select>";
    ?>
</div>

2 Answers 2

3
SHOW DATABASES WHERE `Database` LIKE 'solarsystem%'

Or

SELECT SCHEMA_NAME
FROM information_schema.SCHEMATA
WHERE SCHEMA_NAME LIKE 'solarsystem%'
Sign up to request clarification or add additional context in comments.

Comments

2

Simple solution that comes in mind:

while ($dbrow = mysql_fetch_array($dbresult)) {
    if (strpos($dbrow[0], 'solarsystem') === 0) {
        echo "<option input value='$dbrow[0]'>" .$dbrow[0]."<br></option>";
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.