主鍵 - php中mysql重複插入沒有捕獲到異常?

來源:互聯網
上載者:User
關鍵字 php mysql 主鍵 異常
 function sql_add($name,$db_handle)    {        try{             $statement = $db_handle->prepare            (                'INSERT INTO qiangpiao(name,xuehao,xiaoqu,zuoweihao,time) VALUES (:name,:xuehao,:xiaoqu,:zuoweihao,:time)'            );            $number = $db_handle->prepare            (                'select count(1) from qiangpiao'              );            $number->execute();            /*變數的定義*/            $matches = array();            $matches = fenci($name);            $name = $matches[1];            $xuehao = $matches[2];            $xiaoqu = $matches[3];            $zuoweihao = $number->fetch();//通過資料庫來增加資料            echo $zuoweihao[0];            echo "\n";            $time = date("Y-m-d H:i:s",time());            echo $time;            /*資料繫結*/            $statement->bindParam(':name',$name);            $statement->bindParam(':xuehao',$xuehao);            $statement->bindParam(':xiaoqu',$xiaoqu);            $statement->bindParam(':zuoweihao',$zuoweihao[0]);            $statement->bindParam(':time',$time);            $statement->execute();             return;        }catch(PODException $e){            echo $e->getMessage();         }    }

在資料庫中,我設定了主鍵為學號,重複的話應該會返回一個異常,但我沒有捕獲到異常,是什麼原因,有沒有什麼解決方案。

回複內容:

 function sql_add($name,$db_handle)    {        try{             $statement = $db_handle->prepare            (                'INSERT INTO qiangpiao(name,xuehao,xiaoqu,zuoweihao,time) VALUES (:name,:xuehao,:xiaoqu,:zuoweihao,:time)'            );            $number = $db_handle->prepare            (                'select count(1) from qiangpiao'              );            $number->execute();            /*變數的定義*/            $matches = array();            $matches = fenci($name);            $name = $matches[1];            $xuehao = $matches[2];            $xiaoqu = $matches[3];            $zuoweihao = $number->fetch();//通過資料庫來增加資料            echo $zuoweihao[0];            echo "\n";            $time = date("Y-m-d H:i:s",time());            echo $time;            /*資料繫結*/            $statement->bindParam(':name',$name);            $statement->bindParam(':xuehao',$xuehao);            $statement->bindParam(':xiaoqu',$xiaoqu);            $statement->bindParam(':zuoweihao',$zuoweihao[0]);            $statement->bindParam(':time',$time);            $statement->execute();             return;        }catch(PODException $e){            echo $e->getMessage();         }    }

在資料庫中,我設定了主鍵為學號,重複的話應該會返回一個異常,但我沒有捕獲到異常,是什麼原因,有沒有什麼解決方案。

PODException好像只會在new POD的時候拋出,其他時候都得判斷execute()的傳回值來確定操作是否成功,並通過errorCode()errorInfo()來擷取錯誤的資訊……

預設不把錯誤當異常拋出,你需要使用 errorCode() 來判斷是否執行成功。
你可以設定
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
這樣來使得 SQL 執行出錯的時候拋出異常。

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.