Fejl i forbindelse med MySQL og PHP

tarj1974
Antal: 62
Tilmeldt:
30-03-2014
User is offline
Fejl i forbindelse med MySQL og PHP

Hej i gruppen.

Koden underst skal lave x antal databaser til Joomla! udvikling. Undskylder de forsvundne indrykninger

Jeg kører følgende kode der skal sætte Bruger og Kodeord på en database, efter databasen er blevet skabt:

$sql= "CREATE USER " . "'" . $LocalUsername . "'@'" . $servername . "'" . " IDENTIFIED BY " . "'" . $LocalPassword . "'";

if ($conn->query($sql) === TRUE) {
echo "\n\n USER and PASSWORD created successfully \n\n";
} else {
die ("\n\n Error creating USER and PASSWORD: " . $conn->error . "\n\n");

Jeg får følgende fejl:

Error creating USER and PASSWORD: Operation CREATE USER failed for 'prefix0'@'localhost'

Hvor ligger fejlen? og hvordan udbedrer man den?

Har inspiration fra:
http://dev.mysql.com/doc/refman/5.5/en/create-user...

bruger MySQL 5.5

Bruger Ubuntu Server 14.04.2

Mvh,
Thomas

Her Følger hele koden, som jeg kører med:
"sudo php -f generate_databases_in_php.php"

Database creation in PHP

<?php
$servername
= "localhost";
$username = "MySQL root";
$password = "Indsaet MySQL root password her";
$startDB= 0;
$endDB= 2;
$PrefixDB="prefix";
$spreadsheetCSV="databases.csv";

// Open speadsheet for append.

$csvFile = fopen($spreadsheetCSV, "a");

// Create connection

$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die(
"\n Connection open failed: \n" . $conn->connect_error);
}

for($LocalDB = $startDB; $LocalDB <= $endDB; $LocalDB++)
{

//$LocalDB=system("mkpasswd 'date +%N' ");

$DatabaseName= $PrefixDB . $LocalDB;

// Create DATABASE

$sql= "CREATE DATABASE " . $DatabaseName ;

if ($conn->query($sql) === TRUE) {
echo
"\n\n DATABASE created successfully \n\n";
} else {
die (
"\n\n Error creating DATABASE: " . $conn->error . "\n\n");
}

//$LocalUsername = substr(system("mkpasswd 'date +%N'"), 0);

$LocalUsername = $DatabaseName;

$LocalPassword = substr(system("mkpasswd 'date +%N'"), 0);

echo "\n\n Local Password is " . $LocalPassword . "\n\n" ;

// Create USER and PASSWORD
// HER GÅR DET GALT!

$sql= "CREATE USER " . "'" . $LocalUsername . "'@'" . $servername . "'" . " IDENTIFIED BY " . "'" . $LocalPassword . "'";

if ($conn->query($sql) === TRUE) {
echo
"\n\n USER and PASSWORD created successfully \n\n";
} else {
die (
"\n\n Error creating USER and PASSWORD: " . $conn->error . "\n\n");
}

// GRANT ALL ON LocalDB.* TO USERNAME

$sql = "GRANT ALL ON " . $DatabaseName . ".*" . " TO " . $LocalUsername . "@" . $servername ;

echo "\n\n" . $sql . "\n\n";

if ($conn->query($sql) === TRUE) {
echo
"\n\n Granting created successfully \n\n";
} else {
die (
"\n\n Error creating GRANTING: " . $conn->error . "\n\n");
}

fputcsv($csvFile, array((string)$LocalDB, $DatabaseName, $LocalUsername, $LocalPassword));

};

// Close database connection

$conn->close();

// Close csv spreadsheet file

fclose($csvFile);
?>

Mvh,
Thomas


marlar
Antal: 2823
Tilmeldt:
05-12-2009
User is offline
Prøv at udskrive alle SQL

Prøv at udskrive alle SQL sætningerne og vis dem her (du kan lave kodeordet om).


Slettet

tarj1974
Antal: 62
Tilmeldt:
30-03-2014
User is offline
Hej Marlar :) $conn = new

Hej Marlar :)

$conn = new mysqli($servername, $username, $password);

$sql= "CREATE DATABASE " . $DatabaseName ;
CREATE DATABASE prefix0 (dette virker)

$sql= "CREATE USER " . "'" . $LocalUsername . "'@'" . $servername . "'" . " IDENTIFIED BY " . "'" . $LocalPassword . "'";
CREATE USER 'prefix0'@'localhost' IDENTIFIED BY 'genereret password' (Både med og uden . ";" til sidst, brokker den sig)
(dette virker ikke)

Den når ikke længere end her til.

tjek: http://dev.mysql.com/doc/refman/5.5/en/create-user...

Mvh,
Thomas :)


tarj1974
Antal: 62
Tilmeldt:
30-03-2014
User is offline
Ahhh! brugerne fandtes

Ahhh! brugerne fandtes allerede
DE er slettede, kører nu bedre! :D


marlar
Antal: 2823
Tilmeldt:
05-12-2009
User is offline
Godt du fik løst problemet!

Godt du fik løst problemet!