PDA

View Full Version : sql queries in php on free hosts



PhilS
July 19th, 2009, 11:00
I'm writing php script that would use sql queries such as CREATE DATABASE, CREATE USER, USE, SOURCE, etc. It would connect to mysql, then create database and import data to it from sql backup file, and then create mysql user and grant appropriate perms for it on this database. While asking about some fixes for my code on other forums someone suggested that some shared hosts might not allow to create and manage databased this way and i can only create them from control panel/phpmyadmin... is this true for free hosts? Would my script not work on free hosts?


<?php
mysql_connect("localhost", "username", "password");

$query = "CREATE DATABASE something;
USE something;
SOURCE path/to/sqlfile.sql;
CREATE USER someuser IDENTIFIED BY PASSWORD 'somepass';
GRANT SELECT,INSERT,UPDATE,DELETE ON something.* TO 'someuser'@'localhost';";

$arr= explode( ';', $query );
foreach( $arr as $command )
{
mysql_query( $command );
}
mysql_close();
?>

Dynash
July 19th, 2009, 15:06
It depends on the free host. Normally they would block you from doing that. They would also deny you from even importing your database like that.

But with your code, I doubt it would work like that. You will need to do those queries as individual ones, not as a group.

PhilS
July 19th, 2009, 20:35
Ok. How could I import sql database from script in a way that would work on free host? Any ideas?

iBrightDev
July 21st, 2009, 15:37
a lot of free host will block that stuff from what i have heard in the past, but, not 100% on that, so, your host should be able to tell you.

do you even have a database on the server yet?

if not, see if you can manually create a database.

if it is something you are making, and not a wordpress database or something like that, you could try to manually create the tables if you already have a database.

...



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');");

Hanratty
September 4th, 2009, 04:10
<?php
mysql_connect("localhost", "username", "password");

$query = "CREATE DATABASE something;
USE something;
SOURCE path/to/sqlfile.sql;
CREATE USER someuser IDENTIFIED BY PASSWORD 'somepass';
GRANT SELECT,INSERT,UPDATE,DELETE ON something.* TO 'someuser'@'localhost';";

$arr= explode( ';', $query );
foreach( $arr as $command )
{
mysql_query( $command );
}
mysql_close();
?>