I know there are many many documentations and tutorials on mysqli_fetch_assoc() function which I read but there is one question I can't seem to find the answer to. By the way I am totally new to PHP just started learning it 3 days ago. What I'm trying to do here is.
1) Connect to the server
2) Choose a database
3) List available databases on the connected server
# Connecting to mySQL server on localhost
# creating host and credentials to login to server
$user = "root";
$pass = "pass123";
$host = "localhost";
$DB = "artechdb";
# Create a connection
$sqlconnect = mysqli_connect($host, $user, $pass, $DB);
echo "Connecting to {$host}....";
# Check if connection successful
if (!$sqlconnect) {
die ("Could not connect to $host");
} else {
print "<br>Connected Successfully to $host<br><br>";
}
print "Lets see what kind of databases we have on our mySQL server.<br>";
$queryString = "SHOW databases";
$result = mysqli_query($sqlconnect, $queryString);
if($result = mysqli_query($sqlconnect, $queryString) ) {
# fetch associative array
while($row = mysqli_fetch_assoc($result) ) {
printf ("%s", $row["Database"] . "<br>");
}
}
My questions is at the end the printf statement contains $row["Database"] where is the word "Database" coming from? I do not have a database named "Database".
I thought since it is an associative array I could access and/or print out my database names using $row[0] ..$row[1] .. etc which does not work. I am banging my head past several hours trying to figure out where the Database keyboard is coming from and why we use it instead of accessing our array using zero based index. Any help really appreciate.
mysqli_querytwice. Just doif($result){- Addor die(mysqli_error($sqlconnect))tomysqli_query()and you'll see what pops out.