0

伙计们。我想制作这个脚本,或者其他具有相同效果的脚本:

<?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);
?>

要创建不同名称的数据库,例如:第一次将创建名为“news1”的数据库,下一次,脚本将做某事。像这样:news1+1 = news2,然后会创建一个名为 news2 的数据库,以此类推……

我希望你明白我的意思,如果你能帮助我做到这一点,我将非常感激......问候,丹尼斯赛多夫。

4

2 回答 2

0

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.

于 2013-02-24T19:14:32.487 回答
0

要做到这一点,我猜你不只是想循环 100 次?

您需要将变量保存到文本文件中,然后再次调用它。

将 $text 写入文件:

$var_str = var_export($text, true);
$var = "<?php\n\n\$$text = $var_str;\n\n?>";
file_put_contents('filename.php', $var);

再次检索它:

include 'filename.php';
echo $text;

全部一起:

<?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\n\n\$$text = $var_str;\n\n?>";
file_put_contents('filename.php', $var);
}
function get_var()
include 'filename.php';
return $text;
}
?>

PS我认为这应该只适用于未测试的打字

于 2013-02-24T19:01:21.617 回答