duanruinong0619 2013-02-24 18:44
浏览 52

无法创建用于创建具有不同名称的数据库的脚本。 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.

  • 写回答

2条回答 默认 最新

  • dream6120 2013-02-24 19:01
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么