更新列会导致数据库错误

时间:2013-04-29 18:45:45

标签: php sql sql-server select sql-update

我试图用开发电子邮件地址替换电子邮件地址,以便我可以测试我的电子邮件发送功能。

下面的函数导致我的网页停止响应,当我尝试查看SQL Server Management 2008中的表时,我无法做到。我认为问题在于

$scrub = sqlsrv_query($conn, "UPDATE " . $tName . " SET DCAEmail='test@test.edu'");
但是,我不确定。整个功能:

function scrubEmail($conn){
   $tableNameList = array();
   $getTableName = sqlsrv_query($conn, "Select Name FROM sys.tables");
   while($row = sqlsrv_fetch_array($getTableName)){
      if($row['Name'] !== "sysdiagrams"){
        $tableNameList[] .= $row['Name'];   
      } 
    }

    foreach($tableNameList as &$tName){
        $hasDCAEmail = sqlsrv_query($conn,  "SELECT DCAEmail from " . $tName  );
        if($hasDCAEmail){
            $scrub = sqlsrv_query($conn, "UPDATE " . $tName . " SET DCAEmail='test@test.com'");
            if(!$scrub){
                die( print_r( sqlsrv_errors(), true));
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

感谢Raidenance的帮助,我想通了!完整代码如下。我需要在执行新的查询之前提交sql_srv查询。

function scrubEmail($conn){
   $tableNameList = array();
   $getTableName = sqlsrv_query($conn, "Select Name FROM sys.tables");
   while($row = sqlsrv_fetch_array($getTableName)){
        if($row['Name'] !== "sysdiagrams"){
            $tableNameList[] .= $row['Name'];   
        }
   }

    foreach($tableNameList as &$tabName){
        if ( sqlsrv_begin_transaction( $conn ) === false ) {
            die( print_r( sqlsrv_errors(), true ));
        }
        $hasFIELD = sqlsrv_query($conn,  "SELECT COLUMNNAME from " . $tabName  );
        if($hasFIELD){
        }
        else {
            sqlsrv_rollback( $conn );
            echo "hasFIELD Transaction rolled back.<br />";
        }

        while($row = sqlsrv_fetch_array($hasDCAEmail)){
            $scrub = sqlsrv_query($conn, "UPDATE " . $tabName . " SET COLUMN='test@test.edu'");
            if($scrub){     
                sqlsrv_commit( $conn );
                echo "Transaction committed.<br />";
            }
            else {
                sqlsrv_rollback( $conn );
                echo "Transaction rolled back.<br />";
            }           
        }
    } 
 }