0

I am not a coder so my knowledge is extremely limited.

I am trying to create a table that I can edit and delete entries from the, I found someone's code online and am trying to modify it for myself. My table knows there are x# of rows, but it will not display what the values are. the are of code error is:

require_once('db.php');
$result = $conn->prepare("SELECT * FROM bustracker ORDER BY id ASC");
$result->execute();
for($i=0; $row = $result->fetch(); $i++){
$id=$row['id'];      // **********This is the row the error.log it telling me about This is line 37
        ?>
<tr>
<td style="text-align:center; word-break:break-all; width:300px;"> <?php echo $row ['id']; ?> 
</td>
<td style="text-align:center; word-break:break-all; width:200px;"> <?php echo $row ['date']; ?> 
</td>  
<td style="text-align:center; word-break:break-all; width:200px;"> <?php echo $row ['line']; ?> 
</td> 
<td style="text-align:center; word-break:break-all; width:200px;"> <?php echo $row ['block']; ?> 
</td> 
<td style="text-align:center; word-break:break-all; width:450px;"> <?php echo $row ['start']; ?> 
</td?
<td style="text-align:center; width:350px;">
<a href="edit.php<?php echo '?id='.$id; ?>" class="btn btn-info">Edit</a>
<a href="#delete<?php echo $id;?>"  data-toggle="modal"  class="btn btn-danger" >Delete </a>
</td>

and all the <?php echo $row are the other errors below

The error log says:

[04-Feb-2022 19:06:09 UTC] PHP Warning:  Trying to access array offset on value of type bool in /home/g88pa0itn9u7/bt.inokamanori.com/dataupdate1/index.php on line 37
[04-Feb-2022 19:06:09 UTC] PHP Warning:  Trying to access array offset on value of type bool in /home/g88pa0itn9u7/bt.inokamanori.com/dataupdate1/index.php on line 40
[04-Feb-2022 19:06:09 UTC] PHP Warning:  Trying to access array offset on value of type bool in /home/g88pa0itn9u7/bt.inokamanori.com/dataupdate1/index.php on line 41
[04-Feb-2022 19:06:09 UTC] PHP Warning:  Trying to access array offset on value of type bool in /home/g88pa0itn9u7/bt.inokamanori.com/dataupdate1/index.php on line 42
[04-Feb-2022 19:06:09 UTC] PHP Warning:  Trying to access array offset on value of type bool in /home/g88pa0itn9u7/bt.inokamanori.com/dataupdate1/index.php on line 43
[04-Feb-2022 19:06:09 UTC] PHP Warning:  Trying to access array offset on value of type bool in /home/g88pa0itn9u7/bt.inokamanori.com/dataupdate1/index.php on line 44
[04-Feb-2022 19:06:09 UTC] PHP Warning:  Trying to access array offset on value of type bool in /home/g88pa0itn9u7/bt.inokamanori.com/dataupdate1/index.php on line 56
[04-Feb-2022 19:06:09 UTC] PHP Warning:  Trying to access array offset on value of type bool in /home/g88pa0itn9u7/bt.inokamanori.com/dataupdate1/index.php on line 56
[04-Feb-2022 19:06:09 UTC] PHP Warning:  Trying to access array offset on value of type bool in /home/g88pa0itn9u7/bt.inokamanori.com/dataupdate1/index.php on line 56

This is my site. https://bt.inokamanori.com/dataupdate1/index.php

7-zete-7
  • 712
  • 4
  • 12
  • Change your `for` loop to a `while` loop. `while($row = $result->fetch())` will work much better – aynber Feb 04 '22 at 20:02
  • What type of `$conn` do you have? What engine are you using to connect to the database? – 7-zete-7 Feb 04 '22 at 20:10
  • If your query doesn't return any data then `$row` will be set to false (type bool) which is why you are getting errors. See https://stackoverflow.com/a/59687368/4323201 this answer to a related question. – RyanNerd Feb 04 '22 at 20:12
  • Is this the actual code? According to the current condition in `for`, `for`'s body should not be executed if `$row` is `false`. – 7-zete-7 Feb 04 '22 at 20:19
  • Does this answer your question? [Trying to access array offset on value of type bool in PHP 7.4](https://stackoverflow.com/questions/59674903/trying-to-access-array-offset-on-value-of-type-bool-in-php-7-4) – kmoser Feb 04 '22 at 20:31
  • As I said, I am not a coder, I am just trying to change words that I know to work for my situation. I have copied and pasted that "while" solution above and it didn't like it. What type of $conn, I have no idea. My $row doesn't return any values shown in my table, but it does show me that there are 18 entries in my Database. . – Dennis Lavallee Feb 05 '22 at 02:11

1 Answers1

0

If you use the ->prepare and ->execute approach, your ->fetch does not return the data, but only a bool stating if it is successful or not. Just use ->query and fetch:

$res = $conn->query("SELECT * FROM bustracker ORDER BY id ASC");
while($row = $res->fetch_array()) {
   ...

Here ->fetch_array really delivers your desired data.

crystalAhmet
  • 346
  • 2
  • 7
  • That worked... $res = $conn->query("SELECT * FROM bustracker ORDER BY id ASC"); while($row = $res->fetch_array()) { ... Thank YOU. – Dennis Lavallee Feb 05 '22 at 20:02