0

I am trying to create database and insert SQL file in the same using PHP

But database is not created.

This question is totally different from: Can I mix MySQL APIs in PHP?

As I am not having trouble due to MySQL APIs.

I tried:

$link = mysqli_connect("host", "user", "pass");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
 $usre= "databasename";
 $db_uname = "user";
 $us ="_";
 $db_uname_f = $usre;
// Attempt create database query execution
$sql = "CREATE DATABASE $db_uname_f";
if(mysqli_query($link, $sql)){
    // Name of the file
$filename = 'ch/user_database.sql';
// MySQL host
$mysql_host = 'host';
// MySQL username
$mysql_username = 'user';
// MySQL password
$mysql_password = 'pass';
// Database name
$mysql_database = $db_uname_f;

// Connect to MySQL server
mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
// Select database
mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());

// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line)
{
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
    continue;

// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';')
{
    // Perform the query
    mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
    // Reset temp variable to empty
    $templine = '';
}
}
 $locat = "ch/install_new_user.php?user=";
$locatione = $locat.$usre;
header("Location: $locatione");

} else{



    $msg = "
                   <div class='alert alert-alert'>
                   <button class='close' data-dismiss='alert'>&times;</button>
                      Your Account <strong>$usre</strong> is Now Verified, please wait for email for start operating.
                   </div>
                   ";
}

// Close connection
mysqli_close($link);

Note: It works in wamp.

  • 1
    I have edited out your MySQL login credentials, I suggest you change them **ASAP**, as they're now available for anyone to see! – Qirel Sep 20 '17 at 16:04
  • What version of WAMP are you using that still supports mysql_ functions? – Don't Panic Sep 20 '17 at 16:05
  • 2
    Why are you using a mysqli and mysql connection in the same script? – Don't Panic Sep 20 '17 at 16:06
  • @Don'tPanic, Yes it show me warning for `mysqli` –  Sep 20 '17 at 16:06
  • @Don'tPanic Hope it doesn't matter. –  Sep 20 '17 at 16:07
  • No, as long as you don't actually mix up any of the code between the two connections it should work. Just seems unnecessary. – Don't Panic Sep 20 '17 at 16:08
  • @Don'tPanic so did you got my question? –  Sep 20 '17 at 16:10
  • Yes. Can you show some examples of values that you've tried for `$db_uname_f`? I think that may be the problem. – Don't Panic Sep 20 '17 at 16:12
  • @Don'tPanic Database at `sql209.epizy.com` are created as `usename_databasename` –  Sep 20 '17 at 16:15
  • @Don'tPanic `$usre = "databasename"` , `$db_uname = "username";` , `$us ="_";` , `$db_uname_f = $usre;` –  Sep 20 '17 at 16:16
  • It looks like that will just set `$db_uname_f` to 'databasename' every time. It looks like you intend to concat those variables together to make the name, but there isn't anything that actually does that. Also better quote the name with backticks in case it contains any invalid characters or reserved words. – Don't Panic Sep 20 '17 at 16:20
  • @Don'tPanic but it works in `wamp` –  Sep 20 '17 at 16:22
  • 1
    If you are using free domains, they will not allow you to create the database with `php` as giving permission for that may result in deletion/addition of databases from other users too. For reference visit: https://forum.infinityfree.net/discussion/2158/access-denied#latest – Chirag Jain Sep 21 '17 at 15:24

0 Answers0