-1

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

如何在“如果条件”中检查此更新

4

4 回答 4

0

返回受最后一个INSERTUPDATEREPLACEDELETE查询影响的行数。

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

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

于 2013-06-11T14:42:17.530 回答
0

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

if(mysqli_affected_rows($con) > 0) {
    //update performed
}
于 2013-06-11T14:42:53.123 回答
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 或创建一个类来抽象数据库连接

于 2013-06-11T14:50:05.713 回答
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!';   
    }
于 2013-06-11T14:53:44.187 回答