alanl1 Posted June 26, 2013 Share Posted June 26, 2013 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 More sharing options...
mac_gyver Posted June 26, 2013 Share Posted June 26, 2013 store the query strings in an array. loop over the array to execute each query using one copy of the sqlsrv_prepare/sqlsrv_execute code. Link to comment https://forums.phpfreaks.com/topic/279580-sql-query-is-there-an-easier-way/#findComment-1437906 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.