在php中我在mysql中调用一个过程。现在我想检查查询result == true
是否在我的php文件中设置返回变量为true或false如果查询失败。我怎么能这样做?
这是我的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
此代码运行正确,但我想在查询为true或false时设置返回参数。
答案 0 :(得分:1)
public function DeleteSingleAccount($accountId)
{
$Config = new Config();
$mysqli = $Config->OpenConnection();
return !! mysqli_query($mysqli, "CALL DeleteSingleAccount(" . intval($accountId) . ")");
}
我添加了intval()以避免SQL注入(如果你确定$ accountId总是一个整数,永远不为空,空字符串,用户输入等,则不需要)。
答案 1 :(得分: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!";
}
}