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";
}
?>
4
  • Use a standalone database library,, for example sqlite. Commented Feb 8, 2014 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. Commented Feb 8, 2014 at 15:11
  • You should never connect with root from a php script. Commented Feb 8, 2014 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. Commented Feb 8, 2014 at 15:14

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

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).

Comments

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.