douchunsui2395 2016-02-19 16:36
浏览 52
已采纳

如何使用php安装数据库?

I am currently writing the database install file that automatically installs the database for the user so the Software can run. However, I am just wondering:

Is there a way to just push a SQL file through it?

As of the moment I am having to do:

$tbl_users = "CREATE TABLE Users (
              uid INT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY
              )";

if($db->query($tbl_users) === TRUE) {
    // do the next creation of a table ...

for each table and then manually add the rows as well.

If anyone could just give me some advice it would be appreciated; I am just wondering if the SQL file would be insecure though if it is possible.

Since this is forum software and a lot of people seem to like free hosting when using it, I cannot use shell_exec because it is disabled by most free hosting providers.

Read my comments for a better understanding.

  • 写回答

2条回答 默认 最新

  • dounue6984 2016-02-19 16:47
    关注

    If you are using shared hosting and don't have phpmyadmin or can't upload more than 2MB files or some other problem then you should install MySQL Workbench.

    Create a connection to the mysql server using the settings supplied by the hosting provider and run your sql.

    or to run your SQL script via PHP do the following:

    <?php 
      $db = mysqli_connect("localhost", "root", "my_password", "database"); 
      if (mysqli_connect_errno()) { 
         printf("Connect failed: %s
    ", mysqli_connect_error()); 
         exit();
      } 
      $query = file_get_contents("myfile.sql"); 
      if (mysqli_multi_query($db, $query)) 
         echo "Success"; 
      else
         echo "Fail";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言数据结构实验单链表的删除
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline