5

This code generate database correctly. This code is in C:\inetpub\wwwroot\mini location but it generates the database in C:\ProgramData\MySQL\MySQL Server 5.6\data

How to create database in same place where the code is??

<?php

$conn=new mysqli("localhost","root","hiitisme");
$dept=$conn->escape_string($_POST['dept']);
$class=$conn->escape_string($_POST['class']);
$create=$conn->escape_string($_POST['new']);

if($create=="create")
{
    $sql="CREATE DATABASE $dept";
    $conn->query($sql);
    $sql="USE $dept";
    $conn->query($sql);
    $sql="CREATE TABLE $class(Rollno integer,name varchar(30),phone integer(10),email    varchar(20))";
    $conn->query($sql);
    echo "database createed suceefully";
}
?>
David
  • 1,829
  • 20
  • 31
Pranavadurai
  • 990
  • 2
  • 12
  • 28
  • Use a standalone database library,, for example sqlite. – Niklas B. Feb 08 '14 at 15:08
  • 3
    Even if you could, I would consider it insecure. I think that this is an XY-question. Instead, I suggest that you explain what you are trying to accomplish by doing this. – MasterAM Feb 08 '14 at 15:11
  • You should never connect with *root* from a php script. – Bart Feb 08 '14 at 15:13
  • 2
    Although you technically _can_ give an alternative storage location of InnoDB tables, or in Unix make it a symlink, I'm quite sure this is NOT what you want from MySQL. Please explain your requirements, and I'm sure we can come up with a more standard & robust solution. – Wrikken Feb 08 '14 at 15:14

3 Answers3

2
  • As MasterAM say it is insecure, if i know that , i could download your DB and exploit your's later.
  • If you absolutely want to do that, just change the "datadir" param in my.ini
Taz
  • 78
  • 6
0

You will have to copy your MySQL database after creation. There is no way to relocate it while executing "CREATE DATABASE".

See https://askubuntu.com/questions/14418/different-locations-for-mysql-databases (I know this is for Linux but still same concept).

Community
  • 1
  • 1
Tony Stark
  • 2,318
  • 1
  • 22
  • 41
0

You specify the location of data directory in the mysql configuration file, my.ini, not in the code.

On windows,

1 - stop your server
2 - copy the data directory to the location of your choice 
3 - update my.ini (change data directory)
4 - restart mysql

Linux guide How to change MySQL data directory?

Community
  • 1
  • 1
Nihat
  • 3,055
  • 3
  • 18
  • 28