Retrieving the Name of a database in a HTML form

Hi guys,
I would really appreciate some help, My problem is that I have multiple databases created and I would like to be able to see and select them in an HTML form. But when I try to retrieve the names of the databases I have created my form comes back empty. Is it possible to do this? If so where have I gone wrong and how do I fix the Problem? The code I have come up with is as follows:

<form action="Show_Database_2.php" method="post">
			Registration No.: <select name="registration_no" />
			<option value="<?php 
							$sql = mysqli ("SHOW DATABASES");
							$conn = new mysqli ("localhost", "root", "");
							if ($conn->connect_error) {
								die("connection failed: " . $conn->connect_error);
							}
							if ($conn->query($sql) === TRUE) {
								echo $sql;
							} else { 
								echo "Error showing Databases: " . $conn->error;
							}
							?>">
				<?php
				if ($conn->connect_error) {
					die("connection failed: " . $conn->connect_error);
				}
				echo $sql;	
				?>
			</option>
			
		</form>

Thanks for any help you are able to offer, It will be very much appreciated.

uhhh… well i can tell you your problem’s in this line there chief.

what’s that mysqli and parenthesis doing in that line?

well I was trying all sorts of different methods, and that just happened to be the last one that I used before making this post

But that’s just not how programming works.

On your development machine you always show all errors

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Then you should get at least an “undefined function” error.

Interacting with the database only makes sense after you have the connection.

And as from the documentation

https://www.php.net/manual/en/mysqli.query.php

query() in your situation should never return a boolean true, so a type strict comparison with TRUE would never happen to be valid.

And even if you get to run any query, it’s still a database, and you mostly won’t get any string from it, so echo is the wrong approach to display any data, use var_dump() instead. DATABASES is plural, so you should at least get an array.

You should really start again with learning this. And you could then better switch to PDO:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.