While trying to create a database if not exists, I encountered the following errors, my code can be seen below. It is in two files, the createDatabase.php and the connectionDB.php.
it is showing me this error below:
Fatal error: Uncaught mysqli_sql_exception: Unknown database ‘dictionary’ in
C:xampphtdocsdictionaryconfigcreateDatabase.php:8 Stack trace: #0
C:xampphtdocsdictionaryconfigcreateDatabase.php(8): mysqli_select_db(Object(mysqli),
‘dictionary’) #1 C:xampphtdocsdictionaryconfigcreateDatabase.php(28): checkIfDatabaseExist()
#2 {main} thrown in C:xampphtdocsdictionaryconfigcreateDatabase.php on line 8
createDatabase.php
function checkIfDatabaseExist(){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dictionary";
$conn = mysqli_connect($servername,$username,$password);
if(!mysqli_select_db($conn,$dbname)){
$createDatabase = "CREATE DATABASE IF NOT EXISTS $dbname";
mysqli_query($conn,$createDatabase);
}
mysqli_close($conn);
}
function checkIfTableExist(){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dictionary";
$conn = mysqli_connect($servername,$username,$password,$dbname);
$checkTable = "SELECT 1 FROM dictionary_resource LIMIT 1";
$result = mysqli_query($conn, $checkTable);
if($result){
echo "exist";
}else {
echo "noExist";
}
}
checkIfDatabaseExist();
checkIfTableExist();
?>```
connectionDB.php
``<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dictionary";
$conn = mysqli_connect($servername,$username,$password,$dbname);
if(!conn){
die("Connection Failed: ".mysqli_connect_error());
}
?>`