Mysql create database error

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Mysql create database error

Postby Chikwado » 28. June 2015 20:40

Some one help on advise: while create database my code return the following error:
Code: Select all
 parse error. Syntax, unexpected 'CREATE' (T_STRING) in C: \xampp\htdocs\database.php on line 12
On line 12 are some thing like this:
Code: Select all
$sql = "CREATE  DATABASE mydatabase";
I have tested my code in another server and it works. I am using window 8, Xampp version: 5.6.3. Some one help.
Window8.1 32bits, Code:Block/mingw, web, server, network and latest wine.
User avatar
Chikwado
 
Posts: 39
Joined: 16. July 2014 13:44
Location: Abuja, Nigeria.
XAMPP version: 3.2/php 7.2
Operating System: Window 8.1 32bits

Re: Mysql create database error

Postby glitzi85 » 28. June 2015 20:52

Chikwado wrote:On line 12 are some thing like this

Something is not going to cut it. Please post the exact code at this position (+/- 1 Line)
User avatar
glitzi85
 
Posts: 1920
Joined: 05. March 2004 23:26
Location: Dahoim

Re: Mysql create database error

Postby Chikwado » 29. June 2015 14:56

My code are the following:
Code: Select all
<?php                                        $servername = "localhost";            $username = "root";                         $password = "wizard";                       //Create connection                       $conn = mysqli_connect($servername,$username,$password);                              //Check connection                             if(!$conn) {                                       die("Connection failed:" . mysqli_connect_error());                    }                                                          //Create database                            $sql = "CREATE  DATABASE database";                                          if(mysqli_query($conn,$sql)) {           echo "Database created successfully";                                           }else{                                                 echo "Database not created:" . mysqli_error($conn);                          }                                                         mysqli_close($conn);                        ?>
When I process the code I got the following error:
Code: Select all
parse error. Syntax, unexpected 'CREATE'(T_STRING) in C: \xampp\htdocs\database.php online 12
On line 12 is exactly something like this:
Code: Select all
$sql = "CREATE DATABASE database";
I need help and advise, Thanks.
Window8.1 32bits, Code:Block/mingw, web, server, network and latest wine.
User avatar
Chikwado
 
Posts: 39
Joined: 16. July 2014 13:44
Location: Abuja, Nigeria.
XAMPP version: 3.2/php 7.2
Operating System: Window 8.1 32bits

Re: Mysql create database error

Postby mark.mcdonald » 29. June 2015 15:36

Try this:
Code: Select all
<?php
//conection:
$conn = mysqli_connect("myhost","myuser","mypassw") or die("Error " . mysqli_error($conn));

//consultation:
$query = "CREATE DATABASE somethingotherthandatabase" or die("Error in the consult.." . mysqli_error($conn));

//execute the query.
$result = $conn->query($query);

//display information:
while($row = mysqli_fetch_array($result)) {
  echo $row["name"] . "<br>";
}
mysqli_close($conn);
?>
mark.mcdonald
 
Posts: 160
Joined: 13. March 2015 15:48
Location: Edmonton
Operating System: Windows Server 2012 R2

Re: Mysql create database error

Postby glitzi85 » 29. June 2015 18:19

What are you using to write your code? Your line endings seem to be wrong. This might cause problems (the two spaces between CREATE and DATABASE indicate that).

Also naming a database database is really not a good idea as database is a reserved word. This code works on my machine (tested):

Code: Select all
<?php
$servername = "localhost";
$username = "root";
$password = "wizard";
$conn = mysqli_connect($servername,$username,$password);
if(!$conn) {
   die("Connection failed:" . mysqli_connect_error());
}
$sql = "CREATE DATABASE fancydatabasename";
if(mysqli_query($conn,$sql)) {
   echo "Database created successfully";
}else{
   echo "Database not created:" . mysqli_error($conn);
}
mysqli_close($conn);
?>
User avatar
glitzi85
 
Posts: 1920
Joined: 05. March 2004 23:26
Location: Dahoim


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 138 guests