Phil
Phil

Reputation: 3648

Creating/importing mysql database when host doesn't allow creation of databases from script (php)

I asked similar question before and got some nice code, but i'm concerned with one of comments. I asked how to create and import mysql database from sql file in php on shared hosts. One of commenters said that some hosts might not allow me to do so from php. After digging a bit on other places someone suggested that I could create database from hosts control panel but import it by creating tables via php script executing sql queries this way:

         mysql_query("CREATE TABLE `TABLE NAME HERE` (
        `id` int(25) NOT NULL default '0',
        `CLMN NAME HERE` varchar(250) collate latin1_general_ci default NULL,
        `CLMN NAME HERE` int(10) default NULL,
        PRIMARY KEY  (`id`)
        ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;");

        mysql_query("INSERT INTO `TABLE NAME HERE` VALUES ('VALUE HERE', 'VALUE HERE', 'VALUE HERE');"); 

Could anyone comment on that? How do I achieve what i'm trying to do if given host will have restrictions on what i can do with databases in php?

(I'm not sure if creating new question for that is appropriate, but i thought that since previous question have good examples of doing it if host isn't blocking everything i'll create separate question for doing the same if host is more restrictive.)

Upvotes: 0

Views: 154

Answers (2)

longneck
longneck

Reputation: 12226

shared hosts and frequently do not allow you to create databases, except using their control panel. this is not uncommon, and it's done for good reasons.

and if you can't do it from phpmyadmin or any other administration tool other than their control panel, you won't be able to do it in PHP either.

your only solution in that case is to create the database first using the host's control panel, then edit your backup file and comment out the CREATE DATABASE statement before executing it.

Upvotes: 1

Ben Reisner
Ben Reisner

Reputation: 662

I would be suprised if a host did not allow you to execute CREATE TABLE statements from PHP code.

What was most likely referred was a block on CREATE DATABASE statements. Typically you will have been assigned a database name already, so you would never execute a CREATE DATABASE statement on a shared host.

Upvotes: 0

Related Questions