无法创建用于创建具有不同名称的数据库的脚本。 PHP / MySQL的

guys. I would like to make this script, or other with the same effect:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

if (mysql_query("CREATE DATABASE my_db",$con))
  {
  echo "Database created";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }

mysql_close($con);
?>

To create databases with different names, for example: First time will create database named "news1", the next time, the script will just do sth. like that: news1+1 = news2, and will create database called news2, and so on...

I hope you got my point and I'll really appreciate it if you help me to make this... Regards, Denis Saidov.

To do this I'm guessing that you don't just want to loop 100 times?

You will need to save a variable to a text file then call it again.

Writing $text to a file:

$var_str = var_export($text, true);
$var = "<?php

\$$text = $var_str;

?>";
file_put_contents('filename.php', $var);

Retrieving it again:

include 'filename.php';
echo $text;

All together:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$db_number = get_var();
$my_db = 'news' . $db_number;
if (mysql_query("CREATE DATABASE ".$my_db,$con))
  {
  echo "Database created";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }
$new_db_number = $db_number + 1;
save_var($new_db_number);
mysql_close($con);

function save_var($var) {
$var_str = var_export($text, true);
$var = "<?php

\$$text = $var_str;

?>";
file_put_contents('filename.php', $var);
}
function get_var()
include 'filename.php';
return $text;
}
?>

PS I think that should work only typing not tested

The database should be static. It is a one off event to create the database along with:

  • Tables
  • Indexes
  • Various constraints
  • Triggers
  • Stored procedures

This can be done with a SQL script.

Then the PHP stuff accessing this database only needs to use select, insert, delete or update statements - or as I prefer using stored procedures.

You are going to have a lot of headaches having multiple databases. Also I would hate to have to write a PHP script to do that each time and to administrate them.