Jump to content

SQL Query is there an easier way


alanl1

Recommended Posts

Hi professionals

 

I have some code as follows

 
/*  The queries to be prepared for execution */
/*******************************************************/
$stmt = sqlsrv_query( $conn, $sql);
$stmt1 = sqlsrv_query( $conn, $sql1);
$droptable1 = sqlsrv_query( $conn, $droptable);
$bulkinsert1 = sqlsrv_query( $conn, $bulkinsert);
/*******************************************************/
 
/*  Check the database connection is good */
/*******************************************************/
 if( $conn === false) {
     die( print_r( sqlsrv_errors(), true) );
 }
/*******************************************************/
 
/*  Prepare the drop table query. */
/*******************************************************/
  if(!$droptable1 = sqlsrv_prepare( $conn, $droptable))
  {
    echo "Could not prepare statement";
   die( print_r( sqlsrv_errors(), true));
  }
  /*  Execute the drop table query.  */
 if( !sqlsrv_execute( $droptable1))
  {
        echo "Error in executing statement.\n";
       die( print_r( sqlsrv_errors(), true));   
  }
/*******************************************************/
 
/*  Prepare the Create table statement. */
/*******************************************************/
 if(!$stmt = sqlsrv_prepare( $conn, $sql))
  {
   echo "Could not prepare statement";
   die( print_r( sqlsrv_errors(), true));
  }
  /*  Execute the Create table statement. */
 if( !sqlsrv_execute( $stmt))
  {
        echo "Error in executing statement.\n";
       die( print_r( sqlsrv_errors(), true));   
  }
/*******************************************************/

  
/*  Prepare the BULK insert statement. */
/*******************************************************/
 if(!$bulkinsert1 = sqlsrv_prepare( $conn, $bulkinsert))
  {
    echo "Could not prepare statement";
    die( print_r( sqlsrv_errors(), true));
  }
  /*  Execute the BULK insert statement. */
 if( !sqlsrv_execute( $bulkinsert1))
  {
        echo "Error in executing statement.\n";
       die( print_r( sqlsrv_errors(), true));   
  }
/*******************************************************/
 
/*  Prepare the Cleansing data statement. */
/*******************************************************/
 if(!$stmt1 = sqlsrv_prepare( $conn, $sql1))
  {
    echo "Could not prepare statement";
    die( print_r( sqlsrv_errors(), true));
  }
  /*  Execute the Cleansing data statement. */
 if( !sqlsrv_execute( $stmt1))
  {
        echo "Error in executing statement.\n";
       die( print_r( sqlsrv_errors(), true));   
  }
/******************************************************/
/*  Release the Queries */
/*******************************************************/
sqlsrv_free_stmt( $droptable1);
sqlsrv_free_stmt( $stmt);
sqlsrv_free_stmt( $bulkinsert1);
sqlsrv_free_stmt( $stmt1);
/*******************************************************/
 

is there a better way to prepare, execute and realease them rather than repeating them one by one?

 

 

 

thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/279580-sql-query-is-there-an-easier-way/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.