How do I parse this MySQLi answer?

$tmp = mysqli_query($dbconn, "SELECT * FROM table WHERE id = $id");

Returns:

object(mysqli_result)#3 (5) {
  ["current_field"]=>
  int(0)
  ["field_count"]=>
  int(5)
  ["lengths"]=>
  NULL
  ["num_rows"]=>
  int(1)
  ["type"]=>
  int(0)
}

But.

return mysqli_fetch_array($_tmp, MYSQLI_ASSOC); // with or without 2nd param

Returns:

NULL

I’ve tried to query on SQL database and replaced $id with number, MySQLi query did execute. I also retrieved $id variable itself. It’s an integer, as it should be, containing a number, that I used in manual input as well.

The only thing I could think of is that you are calling fetch twice somehow.

I deleted the double mentioning, now every result repeats :smiley:, what am I doing with my life?!

function Function1($id) {
   global $dbconn;
   $_tmp = mysqli_query($dbconn, "SELECT * FROM summoner WHERE id = $id");
   return mysqli_fetch_array($_tmp);
}

And the answer is:

array(10) {
  [0]=>
  string(8) "76857826"
  ["id"]=>
  string(8) "76857826"
  [1]=>
  string(3) "123"
  ["name"]=>
  string(3) "123"
  [2]=>
  string(3) "123"
  ["revisionDate"]=>
  string(3) "123"
  [3]=>
  string(3) "123"
  ["profileIconId"]=>
  string(3) "123"
  [4]=>
  string(2) "13"
  ["summonerLevel"]=>
  string(2) "13"
}

The row that trigger in MySQLi database and should. Is actually 1 row, database contains 2 rows, but it should print single row. This is the one that should get triggered, repeated for every single step, twice.

What do I do with that?

One is associative eg. “id”
The other is numeric eg. “0”

If you want only one type you could specify that with the optional $resulttype

http://php.net/manual/en/mysqli-result.fetch-array.php

1 Like

YEA!!! Exactly. Thanks!

return mysqli_fetch_array($_tmp, MYSQLI_ASSOC);
Is the one.

But why did they change it? Normally it would provide you associative row.
How did I make it work before? I clearly do not remember having this
obstacle before.

Oh well. Thanks!

It was never changed. You just have to pay more attention to the code you write and always check the manual entry for a function you have a trouble with.

Use mysqli_fetch_assoc instead of mysqli_fetch_array then you won’t need to provide the second argument.

1 Like

Ah. I used assoc. You’re right.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.