I'm new to php. I'm trying to execute the queries below. I know I have to use mysqli_multi_query
but all my tries so far have given me error
. How do I execute these queries? I've read and tried to run other examples with failure.
<?php
include_once 'connect.php';
$user_values = json_encode($_POST['user_values']);//get a json object
$data = json_decode($user_values);
$sql = "";
if (isset($data->phone)) {
$sql .= "CALL new_company_phone('$data->name', $data->phone)";
}
/*procedure that inserts value of "phone" to the corresponding "name" row */
if (isset($data->street)) {
$sql .= "CALL new_company_street('$data->name', '$data->street')";
}
if (isset($data->street_num)) {
$sql .= "CALL new_company_street_num('$data->name' , $data->street_num)";
}
if(isset($data->city)){
$sql .= "CALL new_company_city('$data->name', '$data->city')";
}
if(isset($data->country)){
$sql .= "CALL new_company_country('$data->name', '$data->country')";
}
/* execute multi query */
if (!mysqli_multi_query($con, $sql)) {
echo "error"; }
mysqli_multi_query()
? You could just make several calls tomysqli_query()
.mysqli_multi_query()
makes it difficult to keep track of result sets and errors. I note that your last call appears to be missing a procedure name. FWIW, you would do better to write one update procedure that handles all these different changes rather than having several and making multiple calls for what is essentially one update.mysqli_multi_query()
. I've never seen any legitimat use of it, and it just makes things harder. You also can't use prepared statements, so you open up to SQL injection.