0

I'm trying to select data from a table and update it at the same time. I'm using multiple prepared statements like this:

  mysqli_begin_transaction($link);
  mysqli_autocommit($link, FALSE);
  $getData = mysqli_prepare($link, "select * from tableA where MID=? and IsRead=0");
  mysqli_stmt_bind_param($getData, 'i', $_GET['sid']);
  if(!mysqli_stmt_execute($getData))
  {
    $errorOccurred = true;
  }
  $getData = mysqli_store_result($link);
  mysqli_stmt_close($getData);

  $updateIsRead = mysqli_prepare($link, "update tableA set IsRead=1 where MID=?");
  mysqli_stmt_bind_param($updateIsRead, 'i', $_GET['sid']);
  if(!mysqli_stmt_execute($updateIsRead))
  {
    $errorOccurred = true;
  }
  mysqli_stmt_close($updateIsRead);

  //commit or rollback
  if(!$errorOccurred)
  {
    mysqli_commit($link);
  }
  else {
    mysqli_rollback($link);
  }
  while($dataArray = mysqli_fetch_assoc($getData))
  {
    echo $dataArray['Text'].'<br>';
  }

However, I'm getting this warning: Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given in ...

I checked the manual and found nothing wrong with my code or the data set.

Is there a problem in my code that I'm not aware of?

A-Xepro
  • 29
  • 6
  • Check [the manual about mysqli_store_result()](https://www.php.net/manual/en/mysqli.store-result.php), specifically the section about the return values. _"mysqli_store_result() returns FALSE in case the query didn't return a result set (if the query was, for example an INSERT statement). This function also returns FALSE if the reading of the result set failed."_ – M. Eriksson Mar 29 '20 at 10:11
  • Thanks. I already checked it, but the problem doesn't seem to be related to the result set. I'm sure the data bound matches the data in the table. – A-Xepro Mar 29 '20 at 10:19
  • Show the table structure, something in your query isnt matching the table of your query is malformed. – Grumpy Mar 29 '20 at 10:21
  • What have you tried to debug the problem? Why do you call `mysqli_stmt_close` in the middle of that code and then expect that something might happen with that statement afterwards? – Nico Haase Mar 29 '20 at 10:23
  • The variable `$getData` is used a bit all over. First you make it into a statement (of false on fail), then you store a mysqli result object in it (or false on fail). But right after that, you pass it to `mysqli_stmt_close()`, which doesn't want a result object, but a statement object. Use more variables instead of reusing the same one for different things. – M. Eriksson Mar 29 '20 at 10:27
  • I'm not sure why the question was marked as duplicate. The question they referred to does not describe my problem, nor does the answer solve it! – A-Xepro Mar 29 '20 at 14:16
  • @Grumpy, I've copied the prepared statement and run it via phpMyAdmin, and it worked perfectly there. So, the problem is not with the data. – A-Xepro Mar 29 '20 at 14:18

0 Answers0