Hej i gruppen
Jeg ønsker, at afvikle dette stykke kode i Linux med "PHP -f generate_databases_in_php.php"
Det skal foreløbigt kunne skabe 120 tomme databaser, til Joomla! installationer til web design og udvikling.
Jeg ønsker dog, at printe/skrive værdierne ud til et CSV spreadsheet indeholdende "database nummer," "database navn", "database brugernavn", "database password" og et tomt felt til manuelt at skrive navnet på Joomla! webstedet. Alt dette skal gøres for hver linie som beskriver hver database. Jeg kan dog ikke lige overskue det. Er der nogen, der kan overskue delen med skabelsen af CSV spreadsheetet, som vil kode det?
Mvh,
Thomas.
Database creation in PHP
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Database creation in PHP</title>
</head>
<body>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$startDB= 0;
$endDB= 119;
$PrefixDB="prefixDB";
$spreadsheetCSV="databases.csv";
for($LocalDB = $startDB; $LocalDB <= $endDB; $LocalDB++)
{
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//$LocalDB=system("mkpasswd 'date +%N' ");
$DatabaseName= ${PrefixDB} . ${LocalDB};
// Create DATABASE
$sql= "CREATE DATABASE " . "\'" . ${DatabaseName} . "\'" . ";";
if ($conn->query($sql) === TRUE) {
echo "DATABASE created successfully";
} else {
die ("Error creating DATABASE: " . $conn->error);
}
// Create USER
//$LocalUsername = substr(system("mkpasswd 'date +%N'"), 13);
$LocalUsername = ${DatabaseName};
$sql="CREATE USER " . "\'" . ${LocalUsername} . "\'" . "@" . "\'" . ${servername} . "\'" . ";";
if ($conn->query($sql) === TRUE) {
echo "USER created successfully";
} else {
die ("Error creating USER: " . $conn->error);
}
// Create PASSWORD
$LocalPassword = substr(system("mkpasswd 'date +%N'"), 13);
$sql="IDENTIFIED BY PASSWORD " . "\'" . ${LocalPassword} . "\'" . ";";
if ($conn->query($sql) === TRUE) {
echo "PASSWORD created successfully";
} else {
die ("Error creating PASSWORD: " . $conn->error);
}
// GRANT ALL ON LocalDB.* TO USERNAME
$sql = "GRANT ALL ON " . ${DatabaseName} . ".*" . " TO " . "\'" . ${LocalUsername} . "\'" . "@" . "\'" . ${servername} . "\'" . ";";
if ($conn->query($sql) === TRUE) {
echo "Granting created successfully";
} else {
die ("Error creating GRANTING: " . $conn->error);
}
$conn->close();
};
?>
</body>
</html>