so I have a MySQL database table with the following contents:
---- ---------- --------------------- ------- ------------------ ------------------ ----------
| id | username | created_at | name | time-start | time-end | comments |
---- ---------- --------------------- ------- ------------------ ------------------ ----------
| 1 | test | 2022-11-29 20:23:06 | test | 2022-11-29T22:22 | 2022-11-29T13:23 | NULL |
| 2 | test | 2022-11-29 20:36:51 | test1 | 2022-11-29T22:23 | 2022-11-29T14:12 | NULL |
---- ---------- --------------------- ------- ------------------ ------------------ ----------
I have some code that runs a query on the database:
<?php
require_once "connect.php";
session_start();
$username = $_SESSION['username'];
$sql = "SELECT `name`, `time-start`, `time-end` FROM `reminders` WHERE username = '$username';";
while ($row = mysqli_fetch_assoc(mysqli_query($conn, $sql))){
echo $row["name"] . "<br>";
}
?>
I expect this to code to loop over the results and echo both of the names from the database. Like this:
test
test1
But, instead, i get this:
testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest.....
If anyone knows how to fix, that would be great!
Thanks!
CodePudding user response:
This may not be helpful but it seems that your while loop assigns a value to $row. If this is done successfully, it returns true and your loop runs forever. Try to initialize your $row before hand and then make a for each loop with the resulting list. I'm not familiar with php at all, so I could be completely wrong here, but I figured I'd have a stab. I hope this helped.
CodePudding user response:
Ok so i changed my code to this:
<?php
require_once "connect.php";
session_start();
$username = $_SESSION['username'];
$sql = "SELECT `name`, `time-start`, `time-end` FROM `reminders` WHERE username = '$username';";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
// var_dump($row);
echo $row['name'] . "<br>";
}
?>
And it works now??????????????????
I'm confused