php
  • mysql
  • 2013-06-11 43 views -1 likes 
    -1

    如何檢查使用的查詢是更新我的桌子做或不使用PHP如何檢查更新使用PHP

    下面

    是我的代碼

    $sql_query = "update companies set 
         first_name = '$first_name', 
         last_name = '$last_name', 
         designation = '$designation', 
         company_name = '$company_name', 
         street_address = '$street_address', 
         city_code = '$city_code', 
         telephone_number = '$telephone_number', 
         mobile_number = '$mobile_number', 
         fax_number = '$fax_number' 
         where company_code='1001';"; 
    
        if (!mysqli_query($conn_1,$sql_query)) 
        { 
         $_SESSION['error_details'][0] = 'no'; 
         $_SESSION['error_details'][1] = 'Sorry, not update!'; 
        } 
        else 
        { 
         $_SESSION['error_details'][0] = 'yes'; 
         $_SESSION['error_details'][1] = 'Thank you, update sucessfully!'; 
        } 
    

    如何「如果條件」檢查此更新

    +0

    '如果($ _ SESSION [ 'ERROR_DETAILS'] [0] == '是'){//更新發生}其他{//更新未發生}'? – Sean

    +0

    手冊中沒有提到有關錯誤處理的任何信息嗎? –

    回答

    0

    使用mysqli.affected-rows檢查更新的查詢進行任何更新或沒有。

    if(mysqli_affected_rows($con) > 0) { 
        //update performed 
    } 
    
    0

    返回受上一INSERTUPDATE,行數更換DELETE查詢。

    mysqli_affected_rows()將返回受更新影響的行數。

    http://www.php.net/manual/en/mysqli.affected-rows.php

    0

    PHP如果你正在更新的數據是從你的數據庫中的數據不同,一個好的做法是使用一類或ORM,如果你喜歡純PHP,用類或函數照顧只是將更新正在做的事情就像使用ORM或創建一個類來抽象的數據庫連接

    0

    mysqli_affected_rows()本功能(照顧錯誤將被顯示在立法院的開發時間)

    $result = mysql_query($sqlString) or die(mysql_eror()); // then you will be sure there is no errors; 
    
    if (mysql_affected_rows ($result) <= 0) { 
    //do as you need for no changes on your database 
    } else { 
    //do as you want on database changed 
    } 
    

    不過是更好ion返回以前的SELECT,INSERT,UPDATE,REPLACE或DELETE查詢中受影響的行數。

    瞭解更多:

    http://php.net/manual/en/mysqli.affected-rows.php

    $conn_1 = mysqli_connect("localhost", "DB_USER", "DB_PASSWORD", "DB_NAME"); 
    
        $sql_query = "update companies set 
         first_name = '$first_name', 
         last_name = '$last_name', 
         designation = '$designation', 
         company_name = '$company_name', 
         street_address = '$street_address', 
         city_code = '$city_code', 
         telephone_number = '$telephone_number', 
         mobile_number = '$mobile_number', 
         fax_number = '$fax_number' 
         where company_code='1001'"; 
    
        mysqli_query($conn_1,$sql_query); 
    
        if (mysqli_affected_rows($conn_1)) 
        { 
    
         $_SESSION['error_details'][0] = 'yes'; 
         $_SESSION['error_details'][1] = 'Thank you, update sucessfully!'; 
        } 
        else 
        { 
         $_SESSION['error_details'][0] = 'no'; 
         $_SESSION['error_details'][1] = 'Sorry, not update!'; 
        } 
    
    相關問題