Weird response from normal SQL query

Let’s start with what I expect. This SQL query:
SELECT * FROM champions WHERE id IN (107,60,121,164,245,143,427,64,111,80,134)

This is generated by PHP, but this is the output. This query, when copied to phpMyAdmin (SQL manager). Gives out correct results, which is 11 rows total. Important part of PHP script is:

echo "<pre>";
$result = $dbconn->query($req);

var_dump($result->fetch_assoc());
var_dump($req);
echo "</pre>";

Those are the only 2 var_dump that I have, I sought through entire project with editor (using Find In Project). I have no weird “downloadable extensions”, nor anything else that works as var_dump. Yet that outcome is this:

NULL
string(50) "SELECT id, revision FROM players WHERE id=XXX"
array(5) {
  ["id"]=>
  string(2) "60"
  ["name"]=>
  string(5) "Elise"
  ["title"]=>
  string(16) "the Spider Queen"
  ["icon"]=>
  string(67) "http://ddragon.leagueoflegends.com/cdn/7.1.1/img/champion/Elise.png"
  ["image"]=>
  string(70) "http://ddragon.leagueoflegends.com/cdn/img/champion/splash/Elise_0.jpg"
}
string(78) "SELECT * FROM champions WHERE id IN (107,60,121,164,245,143,427,64,111,80,134)"

Null, that goes away if I remove var_dump($result->fetch_assoc());.
string(50) which is there for some reason (I assign it somewhere else, but it shouldn’t output at all, it should be saved in variable), that goes away if I remove var_dump($req);
Then I have an array(5) which is 1st result given by SQL… but it’s only 1, should be 11.
And then I have string(78), which gets output again!!

Once again, those are only var_dump in the entire project.

You could tell that I have something else assigned to $req. But it would be together in one var_dump, not four seperate.

Well, you’re only dumping the first row from the query result set when you dump $result->fetch_assoc(), so it won’t have 11 rows, just the five elements which are the five columns you retrieve in the query.

1 Like

So how do I retrieve everything?

If it’s PDO, you can create a loop to work through each of the rows, or you can use fetchAll() to retrieve all results into an array: http://php.net/manual/en/pdostatement.fetchall.php - I don’t know mysqli, so I can’t say if there’s an equivalent to fetchall() in there.

1 Like

there is if you use the mysqlnd driver. see http://php.net/manual/en/mysqli-result.fetch-all.php

1 Like

Yep. fetch_all() is there. And works.

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