0

I have a problem with PHP. It gives me this error

Notice: Undefined index: itemEntry in C:\xampp\htdocs\armory\pages\home.php on line 23 Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\armory\pages\home.php on line 26

Here is my code

<?php
function test1() {
    include('config/dbconf.php');

    $sql = "SELECT item FROM character_inventory WHERE guid = 1 AND slot BETWEEN 1 AND 18";
    $result = mysqli_query($conn, $sql);

    while($row = $result->fetch_array()) {
        $rows[] = $row;
    }

    foreach($rows as $row) {
        $sql = "SELECT DISTINCT itemEntry FROM item_instance WHERE guid = " . $row['item'] . "";
        $result = mysqli_query($conn, $sql);

        while($row = $result->fetch_array()) {
            $rows[] = $row;
        }

        foreach($rows as $row) {
            mysqli_select_db($conn, "elunaworld");
            $sql = "SELECT * FROM item_template WHERE entry = " . $row['itemEntry'] . "";
            $result = mysqli_query($conn, $sql);

            if(mysqli_num_rows($result) > 0) {
                while($row = mysqli_fetch_assoc($result)) {
                    echo $row['name'] . "<br>";
                }
            }
        }
    }
}


echo test1();
?>
Tim Levinsson
  • 597
  • 2
  • 7
  • 20
  • What do you get when you do `print_r($rows)` just before the 2nd `foreach($rows...)` line? – BeetleJuice Jul 17 '16 at 03:48
  • That returns Array ( [0] => Array ( [0] => 168 [item] => 168 ) [1] => Array ( [0] => – Tim Levinsson Jul 17 '16 at 03:56
  • And also it still gives the old error about itemEntry Notice: Undefined index: itemEntry in C:\xampp\htdocs\armory\pages\home.php on line 22 – Tim Levinsson Jul 17 '16 at 03:57
  • It's because you're adding to `$rows` which at that point already contains results from the first query. To fix, replace your `while` loops with `$rows = $result->fetch_all()` – BeetleJuice Jul 17 '16 at 04:07
  • Do you mean like this? while($rows = $result->fetch_all()) { $rows[] = $row; } that gives error Notice: Undefined variable: row in C:\xampp\htdocs\armory\pages\home.php on line 8 – Tim Levinsson Jul 17 '16 at 04:14
  • No I said replace the while loops. Take out the while loops and replace them with my line. We're basically filling `$rows` with every result at once, instead of looping through the results and adding them to `$rows` one at a time. – BeetleJuice Jul 17 '16 at 04:16
  • Okey thanks :) But now im getting this error Notice: Undefined index: item in C:\xampp\htdocs\armory\pages\home.php on line 11. And i have not changed anything on line 11 – Tim Levinsson Jul 17 '16 at 04:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117506/discussion-between-beetlejuice-and-user3055512). – BeetleJuice Jul 17 '16 at 04:33
  • There is something very complicated, and probably wrong, with the use of array variables $rows and $row. Did you try to change their names on lines 17, 18, 21, 24, 27 and 28? – Konstantinos Tsourdinis Jul 17 '16 at 05:14

0 Answers0