0

I'm trying to use mysqli_multi_query function but it doesn't work. I think the echo will give me an error to print the $res value, but it print me "1". And so the fetch_array doesn't work because in the first parameter it doesn't accept number 1.

$sql =" start transaction;";
$sql .="insert into account (user_id, username, activation_date, passw) 
values ('$id','$username',CURDATE(),'$password');";
$sql .="insert into users (user_id,name,surname,birth,email,photo) values 
('$id','$name', '$surname','$birth','$mail','$foto');";
$sql .="commit work;";


$res=mysqli_multi_query($connessione, $sql);
echo( $res);
if($res != FALSE)   {   
$row=mysqli_fetch_array($res,MYSQLI_ASSOC); 
echo mysqli_error($connessione);
   return $row;
} else {
print_r(mysqli_error_list($connessione));
echo "<br>";
    mysqli_close($connessione);
   return false;
}

P.S. In mysql WorkBench the values of the query are added to my tables. So i think the query works.

What could it be?

Martin
  • 22,212
  • 11
  • 70
  • 132
MrCaptain Alex
  • 169
  • 1
  • 13
  • what do you mean by "*It doesn't work*"? – James May 28 '18 at 17:51
  • [This Q&A might help you](https://stackoverflow.com/questions/4667596/cannot-figure-out-how-to-run-a-mysqli-multi-query-and-use-the-results-from-the-l) – Martin May 28 '18 at 17:52
  • 1
    What do you want fetch here because there is simply insert query. – Devraj verma May 28 '18 at 17:54
  • 1
    @Martin - I *think* `$res` is a boolean in this case. The documentation (http://php.net/manual/en/mysqli.multi-query.php) says it returns a boolean. @MrCaptain Alex - why are you `mysqli_fetch_array`? There is nothing to fetch; you executed 2 insert statements. – waterloomatt May 28 '18 at 17:54
  • @ Martin There is mysqli_multi_query return true or false so where is mysqli resource here. – Devraj verma May 28 '18 at 17:55
  • @waterloomatt that would entirely fit the `1` / `0` result pattern. And I'd missed the useful comment above yours that the SQL is actually an insert so won't return a `resource` anyway!! `:-)` – Martin May 28 '18 at 17:55
  • @MrCaptain Alex - if `$res` is returning `true` (and it is because when you echo it PHP converts that to `1`) then I'd make a wager that your 2 inserts statements are working. But, I'm still not sure what you're expecting to see with `mysqli_fetch_array`. That function is used for when you're retrieving data, not inserting it. Also, SQL Injection attacks are not your friend. Lookup parameterized queries. If you can explain what you're trying to achieve maybe we can suggest a better approach. – waterloomatt May 28 '18 at 18:02

2 Answers2

1

@MrCaptainAlex you're trying to print the result-set (ie data taken FROM) of an SQL INSERT statement. This is illogical. As stated in comments by cleverer people; your query returns true (1) or false (0) as to if the query executed successfully. It does NOT return any resource or data.

Your SQL code / PHP code is the sort of code that would work with being given data, collected from the database -- a SELECT... FROM rather than an INSERT .... INTO .

You are treating your query $res as if it has just picked up lots of data from the Database. but it has only put data IN TO the database, not etracted any data OUT OF the database. Try running a SELECT ... query instead to pull data FROM the database.

Martin
  • 22,212
  • 11
  • 70
  • 132
-1

you use "echo", but it should be print_r($var) for an array.

Can you use:

print_r($res);

To show us what you have.

Martin
  • 22,212
  • 11
  • 70
  • 132
  • This doesn't really answer the question, not that we're sure what that really is yet, Should be a comment – James May 28 '18 at 17:52