我正在尝试使用database
创建SQL
并插入PHP
个文件
但未创建database
。
此问题完全不同于:Can I mix MySQL APIs in PHP?
由于我没有因MySQL API而遇到麻烦。
我试过了:
$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'>×</button>
Your Account <strong>$usre</strong> is Now Verified, please wait for email for start operating.
</div>
";
}
// Close connection
mysqli_close($link);
注意:适用于wamp
。