Difference between revisions of "CSC220 testSql.php"
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] 21:28, 13 October 2010 (UTC) | --[[User:Thiebaut|D. Thiebaut]] 21:28, 13 October 2010 (UTC) | ||
− | Edited by/on --[[User:Thiebaut|D. Thiebaut]] 11:50, 18 October 2010 (UTC) | + | Edited by/on --[[User:Thiebaut|D. Thiebaut]] 11:50, 18 October 2010 (UTC)<br /> |
+ | Edited by/on --[[User:Thiebaut|D. Thiebaut]] 23:44, 28 October 2010 (UTC) | ||
---- | ---- | ||
<code><pre> | <code><pre> | ||
− | |||
− | |||
// testSql.php | // testSql.php | ||
// D. Thiebaut | // D. Thiebaut | ||
Line 22: | Line 21: | ||
// Globals. Enter your user information here | // Globals. Enter your user information here | ||
//---------------------------------------------------------- | //---------------------------------------------------------- | ||
− | $user | + | $user = "root"; // EDIT THIS LINE! |
− | $passwd | + | $passwd = "voidm%20"; // EDIT THIS LINE TOO! |
− | $host ="xgridmac.dyndns.org"; | + | $host = "xgridmac.dyndns.org"; |
− | $database =" | + | $database = "220a_midterm"; // EDIT THIS LINE AS WELL! |
$sqlFile = "test.sql"; | $sqlFile = "test.sql"; | ||
Line 32: | Line 31: | ||
//---------------------------------------------------------- | //---------------------------------------------------------- | ||
function displayQueryResult( $caption, $result ) { | function displayQueryResult( $caption, $result ) { | ||
− | print "\n---------------------------------------------------------\n"; | + | //print "\n---------------------------------------------------------\n"; |
− | print "Result of query: ". $caption . "\n"; | + | print "===> Result of query: ". $caption . "\n"; |
//var_dump( $result ); | //var_dump( $result ); | ||
if ( $result === True ) { | if ( $result === True ) { | ||
Line 49: | Line 48: | ||
print "\n"; | print "\n"; | ||
} | } | ||
+ | } | ||
+ | |||
+ | //---------------------------------------------------------- | ||
+ | // removeComments: | ||
+ | //---------------------------------------------------------- | ||
+ | function removeComments( $s ) { | ||
+ | $good = explode( "--", $s ); | ||
+ | if ( count( $good )==0 ) | ||
+ | return ""; | ||
+ | return $good[0]; | ||
} | } | ||
Line 69: | Line 78: | ||
//--- remove comments. Comments start a line with '--' --- | //--- remove comments. Comments start a line with '--' --- | ||
$lines = explode( "\n", $oneLine ); | $lines = explode( "\n", $oneLine ); | ||
− | $newLines = array(); | + | //$newLines = array(); |
− | foreach ( $lines as $line ) { | + | //foreach ( $lines as $line ) { |
− | + | // if ( strpos( $line, "--" )===False || strpos( $line, "--" )!=0 ) | |
− | + | // $newLines[] = $line; | |
− | } | + | //} |
//--- split at ; --- | //--- split at ; --- | ||
− | $oneLine = implode( " ", $newLines ); | + | //$oneLine = implode( " ", $newLines ); |
− | $lines = explode( ";", $oneLine ); | + | //$lines = explode( ";", $oneLine ); |
//--- run each query on the SQL server --- | //--- run each query on the SQL server --- | ||
+ | $currentQuery = ""; | ||
foreach( $lines as $line ) { | foreach( $lines as $line ) { | ||
+ | printf( $line . "\n" ); | ||
$line = trim( $line ); | $line = trim( $line ); | ||
− | + | $queryPart = removeComments( $line ); | |
− | if ( strlen( $ | + | if ( strlen( $queryPart )!= 0 ) |
− | + | $currentQuery .= $queryPart; | |
− | + | if ( strchr( $queryPart, ";" )!== False ) { | |
− | + | //print "\n\n=========================================================\n"; | |
− | + | $result = mysql_query( $currentQuery ); | |
− | + | displayQueryResult( "", $result ); | |
− | + | $currentQuery = ""; | |
− | + | printf( "\n" ); | |
− | + | } | |
+ | |||
} | } | ||
Revision as of 18:44, 28 October 2010
--D. Thiebaut 21:28, 13 October 2010 (UTC)
Edited by/on --D. Thiebaut 11:50, 18 October 2010 (UTC)
Edited by/on --D. Thiebaut 23:44, 28 October 2010 (UTC)
// testSql.php
// D. Thiebaut
// A simple php program that reads a file containing sql queries
// and issues them, one at a time to the database server on
// xgridmac.
// The queries are executed, their status displayed (successful,
// unsuccessful), and the result of their output (if any) is also
// displayed on the screen.
// Modification 10/18/10: Julia Burch
// $result = mysql_select_db($database);
//----------------------------------------------------------
// Globals. Enter your user information here
//----------------------------------------------------------
$user = "root"; // EDIT THIS LINE!
$passwd = "voidm%20"; // EDIT THIS LINE TOO!
$host = "xgridmac.dyndns.org";
$database = "220a_midterm"; // EDIT THIS LINE AS WELL!
$sqlFile = "test.sql";
//----------------------------------------------------------
// display the result of the query
//----------------------------------------------------------
function displayQueryResult( $caption, $result ) {
//print "\n---------------------------------------------------------\n";
print "===> Result of query: ". $caption . "\n";
//var_dump( $result );
if ( $result === True ) {
print "query successful\n";
return;
}
if ( $result ===False ) {
print "query unsuccessful\n" ;
return;
}
while ( $row = mysql_fetch_array( $result, MYSQL_NUM ) ) {
for ( $i=0; $i < count( $row ); $i++ )
print "$row[$i]\t";
print "\n";
}
}
//----------------------------------------------------------
// removeComments:
//----------------------------------------------------------
function removeComments( $s ) {
$good = explode( "--", $s );
if ( count( $good )==0 )
return "";
return $good[0];
}
//----------------------------------------------------------
// Run file of SQL queries on MySQL host and report results
//----------------------------------------------------------
//--- connect ---
mysql_connect( $host, $user, $passwd ) or die( "Unable to connect to db\n\n" );
print "==> Connected to database\n";
//--- select database ---
// Modification J. Burch 10/18/10
$result = mysql_select_db($database);
//--- get mysql file ---
$oneLine = file_get_contents( $sqlFile );
//--- remove comments. Comments start a line with '--' ---
$lines = explode( "\n", $oneLine );
//$newLines = array();
//foreach ( $lines as $line ) {
// if ( strpos( $line, "--" )===False || strpos( $line, "--" )!=0 )
// $newLines[] = $line;
//}
//--- split at ; ---
//$oneLine = implode( " ", $newLines );
//$lines = explode( ";", $oneLine );
//--- run each query on the SQL server ---
$currentQuery = "";
foreach( $lines as $line ) {
printf( $line . "\n" );
$line = trim( $line );
$queryPart = removeComments( $line );
if ( strlen( $queryPart )!= 0 )
$currentQuery .= $queryPart;
if ( strchr( $queryPart, ";" )!== False ) {
//print "\n\n=========================================================\n";
$result = mysql_query( $currentQuery );
displayQueryResult( "", $result );
$currentQuery = "";
printf( "\n" );
}
}
//--- done. close connection ---
mysql_close();
?>