1

i have code, and that code generate this syntax:

$sql = "INSERT.....,; INSERT......,; INSERT.....,; INSERT......;";
if(mysqli_multi_query($connection,$sql)){
    echo "SUCCESS!";
}else{
    echo "FAILED!";
}

how to know ALL INSERT IS SUCCESS from mysqli_multi_query()?,

mysqli_multi_query return a bool (true or false), but if i have many INSERT in that function and Only 1 entered from some INSERT, that function is return TRUE.

I want to make sure all the commands run perfectly.

Abdul Aziz Al Basyir
  • 1,104
  • 13
  • 28
  • 6
    Either combine the queries into one INSERT statement if they all insert into the same table or separate them into separate calls so you can check the status of them individually. – John Conde May 08 '17 at 12:36
  • 1
    I think you mean is create many "mysqli_query()" to INSERT many table true @JohnConde? if true, Whether this will make a lot of resources? (i just asking to you :) ) – Abdul Aziz Al Basyir May 08 '17 at 12:41
  • 2
    Are these all the same query or different `INSERT`s? – Machavity May 08 '17 at 12:41
  • 1
    i have different tables each insert.. @Machavity – Abdul Aziz Al Basyir May 08 '17 at 12:43
  • 2
    how about starting a transaction then if one query fail roll back the entire transaction? and use try..catch – Masivuye Cokile May 08 '17 at 12:43
  • 3
    @AbdulAzizAlBasyir There's not that much of a performance hit in running `mysqli_query` multiple times. In fact, I'd say you're less safe with `mysqli_multi_query` since it opens you up to a larger set of SQL injection attacks – Machavity May 08 '17 at 12:45
  • 1
    @Machavity if i use many **mysqli_query()**, whether this will make many resources? – Abdul Aziz Al Basyir May 08 '17 at 12:47
  • 2
    @AbdulAzizAlBasyir Assuming you mean connections to MySQL, the answer is No. If you mean PHP resources, not really. There's a technical hit for calling multiple functions, but unless we're talking thousands of queries, the hit will be negligible – Machavity May 08 '17 at 12:52
  • 2
    why dont you make separate calls and then check if they were successful individually? – Rotimi May 08 '17 at 12:56
  • 1
    If I make it separately, example First insert is success, and second insert failed because many resource in my hosting (example problem connection to mysql, etc) ,If anyone there is another way to make sure everything goes into the database, if any fails, then the system will not check into the database repeatedly any queries that have been entered earlier in order to lighten the server load, I want everything into the database / fail all to the database – Abdul Aziz Al Basyir May 08 '17 at 13:06
  • Don't think this is the correct duplicate for this question – Masivuye Cokile May 08 '17 at 13:17
  • @Masivuye Cokile, if true why i have tag duplicate question from someone..? – Abdul Aziz Al Basyir May 08 '17 at 15:04

0 Answers0