On the contrary! You should never use mysqli_multi_query()
! If you think you found a good use for this function, then you should rethink your approach.
What you should be using are prepared statements with parameter binding. Each CALL()
should be a separate statement. You can create a function to make it easier to call each one if you want.
For example:
function callSP(mysqli $mysqli, string $sql, array $params) {
$stmt = $mysqli->prepare($sql);
$stmt->bind_param(str_repeat("s", count($params)), ...$params);
$stmt->execute();
}
if (isset($data->phone)) {
callSP($con, "CALL new_company_phone(?,?)", [$data->name, $data->phone]);
}
if (isset($data->street)) {
callSP($con, "CALL new_company_street(?,?)", [$data->name, $data->street]);
}
// and so on...
It's difficult to say what your stored procedures actually do. You might have to tweak the way you call them from PHP depending on whether they return results, and whether they use cursors. In general, I would recommend avoiding stored procedures whenever possible; you can do the same in PHP directly.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…