如何检查更新是使用PHP完成的

时间:2013-06-11 14:38:37

标签: php mysql

如何检查使用过的查询是更新我的表还是不使用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!';   
    }

如何在'if condition'

中检查此更新

4 个答案:

答案 0 :(得分:0)

返回受上一次 INSERT 更新 REPLACE DELETE 查询影响的行数。

mysqli_affected_rows()将返回受更新影响的行数。

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

答案 1 :(得分:0)

使用mysqli.affected-rows检查更新的查询是否执行任何更新。

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

答案 2 :(得分:0)

如果你要更新的数据与你数据库中的数据不同,PHP会更新,一个好的做法是使用类或orm,如果你更喜欢普通的php,有类或功能,请注意你做的事情是这样的(在开发时间显示小心错误)

$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
}

然而,最好使用ORM或创建一个类来抽象数据库连接

答案 3 :(得分:0)

mysqli_affected_rows()函数返回上一个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!';   
    }