2014-12-13 108 views
0

在php中我调用了mysql中的一个过程。现在我想检查一个查询result == true然后在我的php文件中设置返回变量,如果查询失败,则返回true或false。我怎样才能做到这一点?当查询结果为真时PHP mysql返回参数

这是我的PHP代码:

public function DeleteSingleAccount($accountId) 
{ 
     $Config = new Config(); 
     $mysqli = $Config->OpenConnection();    
     mysqli_query($mysqli, "CALL DeleteSingleAccount($accountId)") or die("Query fail: ". mysqli_error()); 
} 

这是我在MySQL查询现在:

DELETE 
    FROM table 
     WHERE accountId = par_AccountId 

此代码运行正确的,但我想,当查询是真的设置一个返回参数或假。

+0

你究竟想要检查什么?行是否被删除,执行的查询或查询是否有效? – MatsLindh 2014-12-13 09:34:24

+1

指定这样的查询:'$ result = mysqli_query ...',你可以这样检查:'if($ result){echo“win”; } else {echo“fail”; }(顺便说一下:它可以返回true,但并不意味着它有任何行的init) – Rizier123 2014-12-13 09:35:23

回答

1
public function DeleteSingleAccount($accountId) 
{ 
    $Config = new Config(); 
    $mysqli = $Config->OpenConnection();    
    return !! mysqli_query($mysqli, "CALL DeleteSingleAccount(" . intval($accountId) . ")"); 
} 

我加INTVAL(),以避免SQL注入(不需要如果你确定$帐户ID始终是一个整数,不能为null,空字符串,用户输入等)

0

想想你看对于这样的事情:

public function DeleteSingleAccount($accountId) { 
    $Config = new Config(); 
    $mysqli = $Config->OpenConnection();    
    $result = mysqli_query($mysqli, "CALL DeleteSingleAccount($accountId)") or die("Query fail: ". mysqli_error()); 
    //^Assignment of the query to a variable 

    if($result && mysqli_num_rows($result) > 0) { 
    //^Check if query is successfull ^Check if query has 1 or more rows in it! (You can delete this check if you don't care about how many row's the result has) 
     echo "The query is successful and have 1 or more rows in it!"; 
    } else { 
     echo "The query failed or have 0 rows in it!"; 
    } 

}