Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 12-09-2002, 05:38 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
loading sql schema from php

php/mysql

i'm making a web install script and i'm wondering what the best way to automatically load up sql schema is.

should i read a file into a variable? or is there a function to automatically load sql from a file?
sde is offline   Reply With Quote
Old 12-09-2002, 07:30 PM   #2 (permalink)
sdeming
Code Monkey
 
Join Date: Jul 2002
Location: Michigan
Posts: 85
sdeming is on a distinguished road
Mike, try this by executing:
PHP Code:
run_query_batch($dbhandle"schema.sql"); 
The function:
PHP Code:
function run_query_batch($handle$filename="")
{
  
// --------------
  // Open SQL file.
  // --------------
  
if (! ($fd fopen($filename"r")) ) {
    die(
"Failed to open $filename: " mysql_error() . "<br>");
  }

  
// --------------------------------------
  // Iterate through each line in the file.
  // --------------------------------------
  
while (!feof($fd)) {

    
// -------------------------
    // Read next line from file.
    // -------------------------
    
$line fgets($fd32768);
    
$stmt "$stmt$line";

    
// -------------------------------------------------------------------
    // Semicolon indicates end of statement, keep adding to the statement.
    // until one is reached.
    // -------------------------------------------------------------------
    
if (!preg_match("/;/"$stmt)) {
      continue;
    }

    
// ----------------------------------------------
    // Remove semicolon and execute entire statement.
    // ----------------------------------------------
    
$stmt preg_replace("/;/"""$stmt);

    
// ----------------------
    // Execute the statement.
    // ----------------------
    
mysql_query($stmt$handle) ||
      die(
"Query failed: " mysql_error() . "<br>");

    
$stmt "";
  }

  
// ---------------
  // Close SQL file.
  // ---------------
  
fclose($fd);

sde??? Hrm...
__________________
Scott
B4 09 BA 09 01 CD 21 CD 20 53 63 6F 74 74 24
sdeming is offline   Reply With Quote
Old 12-09-2002, 08:04 PM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
thanks scott, .. i'll post my results.

wow, just noticed my new name looks like yours =P . sde is a nick i've used for a long time.. short for sandiego electronic, .. ( http://sdelectronic.com/ ) a site i used to maintain. i just wanted to cut down on having a few different nicks on the internet. i'm not so concerned with this site, but other sites concern me with having my first initial , last name on there.
sde is offline   Reply With Quote
Old 12-10-2002, 06:54 AM   #4 (permalink)
Admin
$_['Your_Mom'];
 
Admin's Avatar
 
Join Date: May 2002
Location: Santee
Posts: 627
Admin is on a distinguished road
[SDR] SDE kills PrincessMononoke *headshot*

DOH!
__________________


Urban Clothing
Admin is offline   Reply With Quote
Old 12-10-2002, 01:22 PM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
that is actually what i'm making this install for.. it is a server-status plugin that logs to mysql .. then php retrieves the data every 15 seconds.

the install script will walk the web designer through verifying if the db connection info is valid, then creating tables, inserting data, then creating an administrator account so they can change the colors through a web-based interface. it also supports templates and multiple servers. i've spent way too much time on this thing, but i'm almost done with version .01 =)

still haven't used the code above, .. i'm tweeking the database so i don't have to go back later and change it.
sde is offline   Reply With Quote
Old 12-10-2002, 11:07 PM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
Thanks Scott!!

That script worked great!! :rock:
sde is offline   Reply With Quote
Old 12-11-2002, 09:58 AM   #7 (permalink)
sdeming
Code Monkey
 
Join Date: Jul 2002
Location: Michigan
Posts: 85
sdeming is on a distinguished road
Woo hoo, glad to hear it!

When you finish your installer, you should write a NUM outlining the steps.
__________________
Scott
B4 09 BA 09 01 CD 21 CD 20 53 63 6F 74 74 24
sdeming is offline   Reply With Quote
Old 12-11-2002, 06:57 PM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
great idea .. in the mean time, if anyone is interested, .. you can check out what i have done here: http://mystatus.ocsclan.com

i feel a sense of accomplishment with this one.
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Comes of Age sde Code Newbie News 0 04-14-2004 11:41 AM
I need to learn PHP Nitro PHP 9 06-28-2003 11:24 AM
Linux, Php, and Sql Oh My... JeC Linux / BSD / OS X 9 02-28-2003 04:26 AM


All times are GMT -8. The time now is 03:53 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting