how to tell if DB name exist Topic is solved

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

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.
Locked
User avatar
JurajB
Joomla! Guru
Joomla! Guru
Posts: 624
Joined: Fri Oct 02, 2015 3:28 pm

how to tell if DB name exist

Post by JurajB » Mon Oct 18, 2021 9:36 am

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

User avatar
JurajB
Joomla! Guru
Joomla! Guru
Posts: 624
Joined: Fri Oct 02, 2015 3:28 pm

Re: how to tell if DB name exist

Post by JurajB » Mon Oct 18, 2021 10:50 am

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);
I think its clear what I want here

edit: what about this?
https://stackoverflow.com/questions/410 ... n-1-else-2

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2869
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: how to tell if DB name exist

Post by SharkyKZ » Mon Oct 18, 2021 11:12 am

You can use this to get 1 if database exists or 0 if it doesn't:

Code: Select all

SELECT COUNT(*) > 0 FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'nanochat'
It will be either a string or an integer depending on database driver.

User avatar
JurajB
Joomla! Guru
Joomla! Guru
Posts: 624
Joined: Fri Oct 02, 2015 3:28 pm

Re: how to tell if DB name exist

Post by JurajB » Mon Oct 18, 2021 11:47 am

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);
I got this, writes Creating database, when it create it nothing changes, still writes creating...

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

SharkyKZ
Joomla! Hero
Joomla! Hero
Posts: 2869
Joined: Fri Jul 05, 2013 10:35 am
Location: Parts Unknown

Re: how to tell if DB name exist

Post by SharkyKZ » Mon Oct 18, 2021 12:04 pm

You are checking against query string here:

Code: Select all

if ($sqlCheck == 1) {
Should be checking against result. And also this is not related to Joomla coding.

User avatar
JurajB
Joomla! Guru
Joomla! Guru
Posts: 624
Joined: Fri Oct 02, 2015 3:28 pm

Re: how to tell if DB name exist

Post by JurajB » Mon Oct 18, 2021 12:20 pm

please tell me how do I check against result, thanks
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 }

User avatar
JurajB
Joomla! Guru
Joomla! Guru
Posts: 624
Joined: Fri Oct 02, 2015 3:28 pm

Re: how to tell if DB name exist

Post by JurajB » Mon Oct 18, 2021 3:12 pm

cmon I know this is possible somehow

User avatar
JurajB
Joomla! Guru
Joomla! Guru
Posts: 624
Joined: Fri Oct 02, 2015 3:28 pm

Re: how to tell if DB name exist

Post by JurajB » Mon Oct 18, 2021 4:40 pm

got it now after a day of research

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);
edit:
*SOLVED* plugin is on its way


Locked

Return to “Joomla! 4.x Coding”