Unable to display all rows of mysql_fetch_array using while loop.
Hi. My problem is I am only able to display last row entered into database table. I am parsing form data with php to insert into a database table. I know that the form data is being inserted into the table. I am using a while loop to attempt to extract data from two columns in all of the rows of the table. I am using php to display the data of the two columns id & product_name in each row. It is my understanding that a mysql_fetch_array will only return 1 row unless it is used in conjunction with a while loop, which then should return all rows. So I am confused why I can only out put the last row.
When I browse the table in phpMyAdmin there are three rows each with an id number. They are ordered sequentially 1, 2, 3 because the id column is auto increment. id column is primary key. product_name column is unique key.
I don't think there is a problem with my query structure. Possibly a problem defining $product_list variable ? I don't know.
I have closed browser and cleared all history, cookies etc. I have stopped & restarted Apache server & mysql.
When I echo var_dump ($sql); I get: resource(6) of type (mysql result) .
When I echo var_dump ($product_list); I get: string(99) "3 - Blue Jeans edit • delete "
When I print_r ($sqL); I get: Resource id #6
When I echo $product_list; I get: 3 - Blue Jeans edit • delete
I have spent a lot of time searching for answer to this via google searches but none seem to fit this particular problem.
I did see one similar question on stack overflow suggesting using a concatenation approach, but there were zero (0) up-votes. So I wasn't sure if this was a viable remedy to my problem.
I did try adding a dot after $product_list but that resulted in a parse error.
I'm assuming that the loop is overwriting the result over & over until the last one and then that is what is displayed.
I have attached a screen shot of the database in phpMyAdmin.
Here is the code I am using:
//this block grabs the whole list for viewing
$product_list = "";
$sql= mysql_query ("SELECT * FROM `products`");
$product_count = mysql_num_rows ($sql);
if ($product_count > 0) {
while($row = mysql_fetch_array ($sql)) {
$id = $row['id'];
$product_name = $row ['product_name'];
$product_list = "$id - $product_name <a
href='#'>edit</a> • <a href='#'>delete</a>
<br/>";
}
} else {
$product_list = "You have no products listed in your store yet";
}
echo $product_list;
I have also tried mysqli method as follows with same result.
$conn = new mysqli($server, $username, $password, $database);
$product_list = "";
$sql = $conn->query("SELECT * FROM products");
$product_count = mysqli_num_rows ($sql);
if ($product_count > 0) {
while($row = $sql->fetch_assoc()) {
$id = $row['id'];
$product_name = $row ['product_name'];
$product_list = "$id - $product_name <a
href='#'>edit</a> • <a href='#'>delete</a>
<br/>";
}
} else {
$product_list = "You have no products listed in your store yet";
}
echo $product_list;
localhost 127.0.0.1 mystore products phpMyAdmin 4.2.7.1.png