如果affected_rows返回DB类的非对象错误

时间:2015-11-20 14:27:25

标签: php mysql

My Question About DB Class for reference

嗨!在上面的这个问题(链接)中,我遇到了与PHP CLASS连接到DB的问题。现在它正在运作。但是,在我已经在本主题中理解的命令之后,我现在遇到了与affected_rows有关的问题。

$strSQL = $conexao->Query("INSERT INTO clientes(id_cliente,nome,checkout,metodo_pag,valor) VALUES (NULL, 'Nome Cliente', NOW(), '0', '$valorTotal')");

if ($strSQL->affected_rows == 1) {

当我执行它时,PHP返回"尝试在"中获取非对象的属性。

出了什么问题?如果$ conexao->查询已成功完成,那么var $ strSQL检查是不是很好?

2 个答案:

答案 0 :(得分:1)

如果您查看manual中的示例(由Fred -ii-链接),则使用连接:

$result = $mysqli->query("SELECT CountryCode FROM Language");
printf("Affected rows (SELECT): %d\n", $mysqli->affected_rows); // Here
$result->close();

在看了Sam Swift的评论之后,我注意到了我的错误......这是获得你想要的东西的一种方式(在评论中提到)......

首先,在班级Conexao上创建一个函数:

public function affected_rows(){
    return $this->link->affected_rows;
}

然后像这样使用它(在$conexao->Query("INSERT [...]之后):

if($conexao->affected_rows == 1) {
[...]

另请注意mysqli::query的回复:

  

失败时返回FALSE。成功的SELECT,SHOW,DESCRIBE或   EXPLAIN查询mysqli_query()将返回一个mysqli_result对象。对于   其他成功的查询mysqli_query()将返回TRUE。

答案 1 :(得分:1)

我的建议是对查询结果使用验证:

    public static FileSystemRights FileSystemRightsCorrector(FileSystemRights fsRights, bool removeSynchronizePermission = false)
    {
        // from: https://msdn.microsoft.com/en-us/library/aa374896%28v=vs.85%29.aspx
        const int C_BitGenericRead = (1 << 31);
        const int C_BitGenericWrite = (1 << 30);
        const int C_BitGenericExecute = (1 << 29);
        const int C_BitGenericAll = (1 << 28);


        // https://msdn.microsoft.com/en-us/library/aa364399.aspx
        // FILE_GENERIC_READ = FILE_READ_ATTRIBUTES | FILE_READ_DATA | FILE_READ_EA | STANDARD_RIGHTS_READ | SYNCHRONIZE 
        // FILE_GENERIC_WRITE = FILE_APPEND_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_DATA | FILE_WRITE_EA | STANDARD_RIGHTS_WRITE | SYNCHRONIZE
        // FILE_GENERIC_EXECUTE  = FILE_EXECUTE | FILE_READ_ATTRIBUTES | STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE 

        //from Winnt.h
        //#define STANDARD_RIGHTS_READ             (READ_CONTROL)
        //#define STANDARD_RIGHTS_WRITE            (READ_CONTROL)
        //#define STANDARD_RIGHTS_EXECUTE          (READ_CONTROL)

        // from: https://msdn.microsoft.com/en-us/library/windows/desktop/aa379607%28v=vs.85%29.aspx
        // READ_CONTROL = "The right to read the information in the object's security descriptor,"
        // ==> STANDARD_RIGHTS_READ, STANDARD_RIGHTS_WRITE, STANDARD_RIGHTS_EXECUTE == FileSystemRights.ReadPermissions

        // translation for the generic rights to the FileSystemRights enum
        const FileSystemRights C_FsrGenericRead = FileSystemRights.ReadAttributes | FileSystemRights.ReadData | FileSystemRights.ReadExtendedAttributes | FileSystemRights.ReadPermissions | FileSystemRights.Synchronize;
        const FileSystemRights C_FsrGenericWrite = FileSystemRights.AppendData | FileSystemRights.WriteAttributes | FileSystemRights.WriteData | FileSystemRights.WriteExtendedAttributes | FileSystemRights.ReadPermissions | FileSystemRights.Synchronize;
        const FileSystemRights C_FsrGenericExecute = FileSystemRights.ExecuteFile | FileSystemRights.ReadAttributes | FileSystemRights.ReadPermissions | FileSystemRights.Synchronize;

        if (((int)fsRights & C_BitGenericRead) != 0)
        {
            fsRights |= C_FsrGenericRead;
        }

        if (((int)fsRights & C_BitGenericWrite) != 0)
        {
            fsRights |= C_FsrGenericWrite;
        }

        if (((int)fsRights & C_BitGenericExecute) != 0)
        {
            fsRights |= C_FsrGenericExecute;
        }

        if (((int)fsRights & C_BitGenericAll) != 0)
        {
            fsRights |= FileSystemRights.FullControl;
        }

        // delete the 4 highest bits if present
        fsRights = (FileSystemRights)((int)fsRights & ~(C_BitGenericRead | C_BitGenericWrite | C_BitGenericExecute | C_BitGenericAll));

        // for some purposes the "Synchronize" flag must be deleted
        if (removeSynchronizePermission == true)
        {
            fsRights = (FileSystemRights)((int)fsRights & ~((int)FileSystemRights.Synchronize));
        }

        return fsRights;
    }

这使您能够检查它是否有行/不是假等,并使用结果并提供您自己的错误,如果不是

我为MySQLi扩展创建的类并使用found here