0

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.

Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • 2
    Possible duplicate of [Running MySQL \*.sql files in PHP](http://stackoverflow.com/questions/4027769/running-mysql-sql-files-in-php) – Phiter Feb 19 '16 at 16:42
  • Free hosting, can't use: `shell_exec` @PhiterFernandes and I want this to be free for everyone since its a forum software: I know people like to use free hosting for forum Software. – Jaquarh Feb 19 '16 at 16:42
  • Huh? Spend the couple of bucks for better hosting. – patricksweeney Feb 19 '16 at 16:46
  • I have paid hosting, what I am saying is its free forum Software that I am building so I don't want people to be rating it down if there hosting disables the command.... @patricksweeney So I am asking if there is any other ways of doing it.... – Jaquarh Feb 19 '16 at 16:46
  • 1
    Then try something like `$sql = file_get_contents()` and add the sql file to the `file_get_contents` – Phiter Feb 19 '16 at 16:47
  • I'm just finishing work so I'll try this when I get home, thanks for the input @PhiterFernandes – Jaquarh Feb 19 '16 at 16:49
  • Then you need to be clearer about that - this right here: "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." reads like gibberish and is completely unclear. – patricksweeney Feb 19 '16 at 16:52
  • phinx.com is a php module you can install with composer and let's you handle database tables, data inside those tables and versions from the command line. I think it fits your needs. – Borjante Feb 19 '16 at 16:53
  • 1
    Just a side note, if you want to keep a clean record of you database schema, look for some PHP migration packages. They are built to avoid using long, unmaintainable SQL files. – vfsoraki Feb 22 '16 at 02:56

2 Answers2

1

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\n", mysqli_connect_error()); 
     exit();
  } 
  $query = file_get_contents("myfile.sql"); 
  if (mysqli_multi_query($db, $query)) 
     echo "Success"; 
  else
     echo "Fail";
Sohrab
  • 120
  • 8
0

Try this:

<?php
    system('mysql --user=USER --password=PASSWORD DATABASE< file.sql');
?>