Difference between revisions of "CSC220 testSql.php"

From dftwiki3
Jump to: navigation, search
(l)
 
 
(12 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] 21:28, 13 October 2010 (UTC)
 
--[[User:Thiebaut|D. Thiebaut]] 21:28, 13 October 2010 (UTC)
 +
 
----
 
----
  
 
<code><pre>
 
<code><pre>
<?php
+
<?php
 
 
 
// testSql.php
 
// testSql.php
 
// D. Thiebaut
 
// D. Thiebaut
 
// A simple php program that reads a file containing sql queries
 
// A simple php program that reads a file containing sql queries
// and issue them, one at a time to the database server on  
+
// and issues them, one at a time to the database server on  
 
// xgridmac.
 
// xgridmac.
 
// The queries are executed, their status displayed (successful,
 
// The queries are executed, their status displayed (successful,
 
// unsuccessful), and the result of their output (if any) is also
 
// unsuccessful), and the result of their output (if any) is also
 
// displayed on the screen.
 
// displayed on the screen.
 +
// Modification 10/18/10: Julia Burch
 +
// $result = mysql_select_db($database);
 +
// Version 4 (10/30/10)
  
 
//----------------------------------------------------------
 
//----------------------------------------------------------
 
// Globals.  Enter your user information here
 
// Globals.  Enter your user information here
 
//----------------------------------------------------------
 
//----------------------------------------------------------
$user      ="220a-xx";   // EDIT THIS LINE!
+
include 'accessinfo.php';   // contains $user, $password, $host, $database, and $sqlFile
$passwd  = "xxxxxx";      // EDIT THIS LINE TOO!
+
 
$host       ="xgridmac.dyndns.org";
 
$database ="220a_example1";
 
$sqlFile     = "test.sql";
 
  
 
//----------------------------------------------------------
 
//----------------------------------------------------------
Line 27: Line 27:
 
//----------------------------------------------------------
 
//----------------------------------------------------------
 
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 35: Line 35:
 
   }
 
   }
 
   if ( $result ===False ) {
 
   if ( $result ===False ) {
     print "query unsuccessful\n" ;
+
     print "query unsuccessful: " . mysql_error() . "\n" ;
 
     return;
 
     return;
 
   }
 
   }
Line 44: Line 44:
 
     print "\n";
 
     print "\n";
 
   }
 
   }
 +
}
 +
 +
//----------------------------------------------------------
 +
// removeComments:
 +
//----------------------------------------------------------
 +
function removeComments( $s ) {
 +
  $good = explode( "--", $s );
 +
  if ( count( $good )==0 )
 +
      return "";
 +
  return $good[0];
 
}
 
}
  
Line 51: Line 61:
  
 
//--- connect ---
 
//--- connect ---
mysql_connect( $host, $user, $passwd ) or die( "Unable to connect to db\n\n" );
+
$link = mysql_connect( $host, $user, $passwd );
print "==> Connected to database\n";
+
if ( !$link )
 +
  die( "Unable to connect to db\n" . mysql_error() . "\n" );
 +
print "==> Connected to server\n";
  
 
//--- select database ---
 
//--- select database ---
$result = mysql_query( "use ". $database );
+
// Modification J. Burch 10/18/10
 +
$result = mysql_select_db($database);
 +
if ( ! $result )
 +
  die( "unable to select database " . $database . "\n" . mysql_error() .  "\n" );
 +
 
 +
print "--> connected to " . $database . "\n";
  
 
//--- get mysql file ---
 
//--- get mysql file ---
Line 62: Line 79:
 
//--- remove comments.  Comments start a line with '--' ---
 
//--- remove comments.  Comments start a line with '--' ---
 
$lines = explode( "\n", $oneLine );
 
$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 ---
 
//--- run each query on the SQL server ---
 +
$currentQuery = "";
 
foreach( $lines as $line ) {
 
foreach( $lines as $line ) {
 +
  printf( str_replace( "%", "%%", $line  ). "\n" );
 
   $line = trim( $line );
 
   $line = trim( $line );
   //--- skip blank lines ---
+
   $queryPart = removeComments( $line );
   if ( strlen( $line ) < 2 )
+
   if ( strlen( $queryPart )!= 0 )
    continue;
+
    $currentQuery .= " " . $queryPart;
 
+
    if ( strchr( $queryPart, ";" )!== False ) {
  //--- display result ---
+
      $result = mysql_query( $currentQuery );
  print "\n\n=========================================================\n";
+
      displayQueryResult( "", $result );
  $newLine = wordwrap( $line );
+
      $currentQuery = "";
  print "query = $newLine\n";
+
      printf( "\n" );
  $result = mysql_query( $line );
+
    }
  displayQueryResult( "", $result );
+
 
 
}
 
}
  
Line 91: Line 101:
  
 
?>
 
?>
 
  
  
Line 107: Line 116:
 
<br />
 
<br />
 
<br />
 
<br />
[[Category:CSC220]][[Category:Php]][[Category:MySQL]]
+
[[Category:CSC220]][[Category:Php]][[Category:MySql]]

Latest revision as of 08:32, 31 October 2010

--D. Thiebaut 21:28, 13 October 2010 (UTC)


 <?php
// 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);
// Version 4 (10/30/10)

//----------------------------------------------------------
// Globals.  Enter your user information here
//----------------------------------------------------------
include 'accessinfo.php';    // contains $user, $password, $host, $database, and $sqlFile


//----------------------------------------------------------
// 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: " . mysql_error() . "\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 ---
$link = mysql_connect( $host, $user, $passwd );
if ( !$link ) 
   die( "Unable to connect to db\n" . mysql_error() . "\n" );
print "==> Connected to server\n";

//--- select database ---
// Modification J. Burch 10/18/10
$result = mysql_select_db($database);
if ( ! $result ) 
   die( "unable to select database " . $database . "\n" . mysql_error() .  "\n" );

print "--> connected to " . $database . "\n";

//--- get mysql file ---
$oneLine = file_get_contents( $sqlFile );

//--- remove comments.  Comments start a line with '--' ---
$lines = explode( "\n", $oneLine );

//--- run each query on the SQL server ---
$currentQuery = "";
foreach( $lines as $line ) {
  printf( str_replace( "%", "%%", $line  ). "\n" );
  $line = trim( $line );
  $queryPart = removeComments( $line );
  if ( strlen( $queryPart )!= 0 )
     $currentQuery .= " " . $queryPart;
     if ( strchr( $queryPart, ";" )!== False ) {
       $result = mysql_query( $currentQuery );
       displayQueryResult( "", $result );
       $currentQuery = "";
       printf( "\n" );
     }
  
}

//--- done.  close connection ---
mysql_close();

?>