how to tell if DB name exist Topic is solved
Moderators: ooffick, General Support Moderators
Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
- JurajB
- Joomla! Guru
- Posts: 624
- Joined: Fri Oct 02, 2015 3:28 pm
how to tell if DB name exist
how to tell if DB name exist or not
I found this:
$sqlCheck = "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'nanochat'
cant get true or false from it
I found this:
$sqlCheck = "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'nanochat'
cant get true or false from it
- JurajB
- Joomla! Guru
- Posts: 624
- Joined: Fri Oct 02, 2015 3:28 pm
Re: how to tell if DB name exist
Code: Select all
<?php
$serverName = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "nanochat";
$conn = mysqli_connect($serverName, $dBUsername, $dBPassword);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$createDb = "CREATE DATABASE nanochat";
$sqlCheck = "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'nanochat'";
$sqlCheckEmpty = empty($sqlCheck);
if (mysqli_query($conn, $sqlCheckEmpty)) {
mysqli_query($conn, $createDb);
echo "Creating database";
} else {
echo "Database already exist";
}
mysqli_close($conn);
edit: what about this?
https://stackoverflow.com/questions/410 ... n-1-else-2
-
- Joomla! Hero
- Posts: 2869
- Joined: Fri Jul 05, 2013 10:35 am
- Location: Parts Unknown
Re: how to tell if DB name exist
You can use this to get 1 if database exists or 0 if it doesn't:
It will be either a string or an integer depending on database driver.
Code: Select all
SELECT COUNT(*) > 0 FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'nanochat'
- JurajB
- Joomla! Guru
- Posts: 624
- Joined: Fri Oct 02, 2015 3:28 pm
Re: how to tell if DB name exist
Code: Select all
<?php
$serverName = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "nanochat";
$conn = mysqli_connect($serverName, $dBUsername, $dBPassword);
$sqlCheck = "SELECT COUNT(*) > 0 FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'nanochat'";
$createDb = "CREATE DATABASE nanochat";
$queryCreate = mysqli_query($conn,$createDb);
$queryCheck = mysqli_query($conn, $sqlCheck);
$conn;
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if ($sqlCheck == 1) {
echo "Database already exist";
} else {
$createDb;
echo "Creating database";
}
mysqli_close($conn);
edit: I hope its not bad for my 3rd day live programming app from basic lectures I have been studying so far.
edit2: Its mariadb 10.4 xampp 8.0.11
-
- Joomla! Hero
- Posts: 2869
- Joined: Fri Jul 05, 2013 10:35 am
- Location: Parts Unknown
Re: how to tell if DB name exist
You are checking against query string here:
Should be checking against result. And also this is not related to Joomla coding.
Code: Select all
if ($sqlCheck == 1) {
- JurajB
- Joomla! Guru
- Posts: 624
- Joined: Fri Oct 02, 2015 3:28 pm
Re: how to tell if DB name exist
please tell me how do I check against result, thanks
working on it
edit: cant get it working, please help - I just googled this:
working on it
edit: cant get it working, please help - I just googled this:
Code: Select all
if (mysqli_num_rows($result)==0) { PERFORM ACTION }
- JurajB
- Joomla! Guru
- Posts: 624
- Joined: Fri Oct 02, 2015 3:28 pm
Re: how to tell if DB name exist
cmon I know this is possible somehow
- JurajB
- Joomla! Guru
- Posts: 624
- Joined: Fri Oct 02, 2015 3:28 pm
Re: how to tell if DB name exist
got it now after a day of research
edit:
*SOLVED* plugin is on its way
Code: Select all
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "nanochat";
$conn = new mysqli($servername, $username, $password);
if (empty (mysqli_fetch_array(mysqli_query($conn,"SHOW DATABASES LIKE '$dbname'"))))
{
echo "DB not exist<br>";
echo "Creating DB";
mysqli_query($conn, "CREATE DATABASE nanochat");
}
else
{
echo "DB exist<br>";
}
mysqli_close($conn);
*SOLVED* plugin is on its way