Save sqlcreatedb.php as sqlshowdbs.php. This new page will display all databases on your server. If you were able to create the new database ywstutorial it should show that one as well. If not you will see the ones your provider has given you.
In sqlshowdbs.php replace the query which currently says this:
mysqli_query($dbconnection, "CREATE DATABASE ywstutorial;");
with this new query:
$sqlResult=mysqli_query($dbconnection, "SHOW DATABASES;");
Two things have changed. One is that you have a variable (actually an array) to hold the result of this query. The other is the query text itself. That query will list all of the databases on the server which you have access to. To finish the page you need to use PHP to put the list into the Web page so these lines:
$sqlRow=mysqli_fetch_array($sqlResult);
print_r($sqlRow);
This should work but you might need an explanation. Save, upload and try the page and then read what each line of code means:
- you connect to the database server
- you ask the server to make a list of all the databases and store that list but not in the variable as that becomes a pointer to the results which stay on the server
- you use another PHP function and provide it with the pointer to the results on the database server and mysqli_fetch_array() asks for just the first line in the list of databases which is then store din the variable $sqlRow
- you output $sqlRow to the Web page
$sqlRow=mysqli_fetch_array($sqlResult);
print_r($sqlRow);
You should have got a result something like this when you tried the page:
Array ( [0] => information_schema [Database] => information_schema )
What you did is said to the server "Give me the first row from the list of databases". That first row shown above is a table called information_schema but yours might be something else.
The output is not especially friendly so to make it easier to read add these echo lines around the existing print_r line:
echo "<pre>";
print_r($sqlRow);
echo "/<pre>";
You are now using PHP to talk to an SQL server and also to generate HTML. In just five lines of code you are using three different languages. This is why Web development can become very confusing and why you need to make sure you learn each language carefully.
All the data - the wrong way
At the moment you are seeing just the first database. You want to see them all. Copy the five lines which get a row of data and display it using mysqli_fetch_array and print_r (not the connection line). Paste those lines under themselves so you have two identical blocks of four lines. Save, upload and try the page and you should see two databases. Keep doing that until no more databases exist (the last one will probably be the ywstutorial database you created if your server let you).
All the data - the right way
Copying those four lines works but you do not know how many copies you need. This is a perfect place to use a loop. In the PHP tutorial you learned about while loops but they were no real use. Now you can use one. You should currently have four lines of code repeated may times. compare this code with those lines:
while ($sqlRow=mysqli_fetch_array($sqlResult)){
echo "<pre>";
print_r($sqlRow);
echo "</pre>";
}
The while loop is new to this page but should be recognisable - look for the brackets and the curly brackets (braces). Inside the normal brackets is the same code you had before. Inside the curly brackets are the same three lines you had before.
Delete all of the lines of code except for the connect line and the query line. In their place put the above five lines. Save, upload and try.
Instead of showing just the first row from the database it will display as many as there are. The while loop condition is the code which fetches data. All functions return a value. So when you call the function mysqli_fetch_array() it returns the row of data from the server and that is stored in the variable. However, when it fails to get another row of data (because there are no more databases to list) the function will return the special value false instead. The while loop carries on until it gets the response false. That means it will loop through as many times as there are databases on the server and then stop.