Last active
June 16, 2022 05:58
-
-
Save catwhocode/5ce4511f007b6720c9bafa3f4fdeed0c to your computer and use it in GitHub Desktop.
PHP: Create MySQL Database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
$mysqli = new mysqli('localhost', 'root', '', ''); | |
if ($mysqli->connect_errno) { | |
throw new RuntimeException('mysqli connection error: ' . $mysqli->connect_error); | |
} | |
/* Set the desired charset after establishing a connection */ | |
$mysqli->set_charset('utf8'); | |
if ($mysqli->errno) { | |
throw new RuntimeException('mysqli error: ' . $mysqli->error); | |
} | |
?> | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>.</title> | |
<style> | |
a {text-decoration:none;} | |
.message {font-weight: bold;color: red;} | |
li {font-family: consolas;font-size: larger;padding: 5px;} | |
ol > li:nth-of-type(odd){background-color: #eee;} | |
.inputSection,input {font-family: consolas;font-size: 18px;padding: 5px;} | |
</style> | |
</head> | |
<body> | |
<h1><a href="index.php">Create New MySQL DB</a></h1> | |
<?php | |
if (isset($_SESSION['message'])){ | |
echo '<p class="message">' . $_SESSION['message'] . '</p>'; | |
unset($_SESSION['message']); | |
} | |
?> | |
<form method="post" action="process.php"> | |
<div class="inputSection">Database Name: <input name="database_name"> <input type="submit" value="Create Database"></div> | |
</form> | |
<?php | |
$result = $mysqli->query('SHOW databases') or trigger_error('connect failed: '.join(',', $mysqli->error_list), E_USER_ERROR); | |
echo '<ol>'; | |
foreach( $result as $row ) { | |
echo '<li>' . $row['Database'] . '</li>'; | |
} | |
echo '</ol>'; | |
?> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// error_reporting(0); | |
// mysqli_report(MYSQLI_REPORT_OFF); | |
session_start(); | |
$mysqli = new mysqli('localhost', 'root', '', ''); | |
if ($mysqli->connect_errno) { | |
throw new RuntimeException('mysqli connection error: ' . $mysqli->connect_error); | |
} | |
/* Set the desired charset after establishing a connection */ | |
$mysqli->set_charset('utf8'); | |
if ($mysqli->errno) { | |
throw new RuntimeException('mysqli error: ' . $mysqli->error); | |
} | |
if (isset($_POST['database_name'])){ | |
$databaseName = $_POST['database_name']; | |
$sql = "CREATE DATABASE IF NOT EXISTS $databaseName CHARACTER SET utf8 COLLATE utf8_general_ci;"; | |
$mysqli->query($sql); | |
$_SESSION['message'] = 'Database created successfully...'; | |
} | |
header("location:index.php"); | |
/* end of file */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment