Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Setting up database install file
Old 05-29-2009, 06:43 PM Setting up database install file
lothop's Avatar
Ultra Talker

Posts: 303
Trades: 0
Hi people,

I have created a small offline program. How can I quickly install the database on someone Else's computer from a php file.

" databaseinstall.php "
So when they view that in their browser it sets up the database.

I have the database dump information.
Code:
-- phpMyAdmin SQL Dump
-- version 3.1.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 30, 2009 at 10:34 AM
-- Server version: 5.1.30
-- PHP Version: 5.2.8

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `name`
--
CREATE DATABASE `name` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `name`;

-- --------------------------------------------------------

--
-- Table structure for table `veges`
--

CREATE TABLE IF NOT EXISTS `veges` (
  `id` int(2) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `M` tinyint(2) NOT NULL,
Will I have to type everything up with php, running queries? or is there a faster way to do this straight from the SQL file?
__________________
Websites Created;
warscope.com
ratepayers.org.nz

Last edited by lothop; 05-29-2009 at 10:48 PM..
lothop is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-30-2009, 12:21 AM Re: Setting up database install file
nayes84's Avatar
Extreme Talker

Latest Blog Post:
Difference between ASP And JSP
Posts: 232
Name: John
Location: Tokyo
Trades: 0
you can dump the file into your database through shell.
if you can't access shell on the remote computer. you can use exec function to execute the command on shell
PHP Code:
exec("mysql dbname < file.sql"); 
ps. you may need to provide username and password for accessing the database. it depends on the settings on your server
PHP Code:
exec("mysql --user=user --password=pass dbname < file.sql"); 
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

if(I'm("Helpful")) Add_Talkupation("nayes84");
nayes84 is offline
Reply With Quote
View Public Profile
 
Old 05-30-2009, 01:37 AM Re: Setting up database install file
lothop's Avatar
Ultra Talker

Posts: 303
Trades: 0
Opps, read below please
__________________
Websites Created;
warscope.com
ratepayers.org.nz

Last edited by lothop; 05-30-2009 at 06:05 AM..
lothop is offline
Reply With Quote
View Public Profile
 
Old 05-30-2009, 06:04 AM Re: Setting up database install file
lothop's Avatar
Ultra Talker

Posts: 303
Trades: 0
Umm, edit that, its no longer working? for some reason?

This is the PHP
PHP Code:
<?php
$link 
mysql_connect('localhost''root''admin');
if (!
$link) {
    die(
'Could not connect: ' mysql_error());
}

exec("mysql fruit < database.sql"); 
?>
And this is the start of the database.sql
Code:
-- phpMyAdmin SQL Dump
-- version 3.1.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 30, 2009 at 10:34 AM
-- Server version: 5.1.30
-- PHP Version: 5.2.8

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `fruit`
--
CREATE DATABASE `fruit` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `fruit`;

-- --------------------------------------------------------

--
-- Table structure for table `kindsoffruit`
--

CREATE TABLE IF NOT EXISTS `kindsoffruit` (

  `name` varchar(255) NOT NULL

) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ;

--
-- Dumping data for table `kindsoffruit`
--

INSERT INTO `kindsoffruit` (`name`) VALUES
('apple');
For some reason it isn't working.
__________________
Websites Created;
warscope.com
ratepayers.org.nz
lothop is offline
Reply With Quote
View Public Profile
 
Old 05-30-2009, 07:33 AM Re: Setting up database install file
lothop's Avatar
Ultra Talker

Posts: 303
Trades: 0
I found this script after hours of search that works well.

PHP Code:

<?php
error_reporting
(E_ALL);
/*
 * Restore MySQL dump using PHP
 * (c) 2006 Daniel15
 * Last Update: 9th December 2006
 * Version: 0.2
 * Edited: Cleaned up the code a bit. 
 *
 * Please feel free to use any part of this, but please give me some credit :-)
 */
 
// Name of the file
$filename 'system\database.sql';
// MySQL host
$mysql_host '';
// MySQL username
$mysql_username '';
// MySQL password
$mysql_password '';
// Database name
$mysql_database '';
 
//////////////////////////////////////////////////////////////////////////////////////////////
 
// Connect to MySQL server
mysql_connect($mysql_host$mysql_username$mysql_password) or die('Error connecting to MySQL server: ' mysql_error());

//Create database


// Select database

 
// Temporary variable, used to store current query
$templine '';
// Read in entire file
$lines file($filename);
// Loop through each line
foreach ($lines as $line)
{
    
// Skip it if it's a comment
    
if (substr($line02) == '--' || $line == '')
        continue;
 
    
// Add this line to the current segment
    
$templine .= $line;
    
// If it has a semicolon at the end, it's the end of the query
    
if (substr(trim($line), -11) == ';')
    {
        
// Perform the query
        
mysql_query($templine) or print('Error performing query \'<strong>' $templine '\': ' mysql_error() . '<br /><br />');
        
// Reset temp variable to empty
        
$templine '';
    }
}
 
?>
__________________
Websites Created;
warscope.com
ratepayers.org.nz
lothop is offline
Reply With Quote
View Public Profile
 
Old 05-31-2009, 03:40 AM Re: Setting up database install file
nayes84's Avatar
Extreme Talker

Latest Blog Post:
Difference between ASP And JSP
Posts: 232
Name: John
Location: Tokyo
Trades: 0
that script just divide sql file and run each query separately. personally I prefer to use mysql dump functionality especially for large files since it is quite efficient and faster than just dividing dump file and running each query one by one.

concerning mysql dump functionality. try adding username and password for mysql line like I posted above. this should be supported where ever mysql is installed
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

if(I'm("Helpful")) Add_Talkupation("nayes84");
nayes84 is offline
Reply With Quote
View Public Profile
 
Old 05-31-2009, 08:13 AM Re: Setting up database install file
lothop's Avatar
Ultra Talker

Posts: 303
Trades: 0
I tried your way and added the username and password to it and it didnt seem to work. Still..
__________________
Websites Created;
warscope.com
ratepayers.org.nz
lothop is offline
Reply With Quote
View Public Profile
 
Old 06-05-2009, 10:00 PM Re: Setting up database install file
lothop's Avatar
Ultra Talker

Posts: 303
Trades: 0
Would it be that it your way isnt working because it has comment lines in the sql file?
__________________
Websites Created;
warscope.com
ratepayers.org.nz
lothop is offline
Reply With Quote
View Public Profile
 
Old 06-06-2009, 04:48 AM Re: Setting up database install file
nayes84's Avatar
Extreme Talker

Latest Blog Post:
Difference between ASP And JSP
Posts: 232
Name: John
Location: Tokyo
Trades: 0
mysql dump should work even with comments.

I used it for long time and it works without any problems.

can you access shell? may be you should try running it directly from shell.
if your php setting is on safe mode, exec function will not work.
Quote:
Originally Posted by lothop View Post
Would it be that it your way isnt working because it has comment lines in the sql file?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

if(I'm("Helpful")) Add_Talkupation("nayes84");
nayes84 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Setting up database install file
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.34212 seconds with 12 queries