slj90 Posted October 11, 2015 Share Posted October 11, 2015 I'm trying to display data from a MySQL table using PHP. At the minute it is repeating the frist row 23 times, there are 23 rows in the table. What do I need to add to my code to make it display each row? $result=mysqli_query($con,"SELECT item_item_title, item_username FROM items"); $row = mysqli_fetch_array($result); $item_item_title = $row[item_item_title]; $item_username = $row[item_username]; if (mysqli_num_rows($result) > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { echo $item_item_title . " - " . $item_username . "<br>"; } } else { echo "0 results"; } Thanks Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted October 11, 2015 Share Posted October 11, 2015 You need to fix the logic of your code. Right now, you fetch the first row (which may not even exist) and extract the item title and username. Then you iterate over the remaining rows, but you just keep echoing the title and username of the first row. Get rid of the extra code for the first row and extract the title and name for each row within the loop. Quote Link to comment Share on other sites More sharing options...
mlukac89 Posted October 12, 2015 Share Posted October 12, 2015 (edited) Try change to this $result=mysqli_query($con,"SELECT item_item_title, item_username FROM items"); if (mysqli_num_rows($result) > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { echo $row[item_item_title] . " - " . $row[item_username] . "<br>"; } } else { echo "0 results"; } Edited October 12, 2015 by mlukac89 Quote Link to comment Share on other sites More sharing options...
Ani123 Posted January 1, 2016 Share Posted January 1, 2016 Hey,try this code: $query="SELECT * FROM MY_TABLE";$results = mysql_query($query);while ($row = mysql_fetch_array($results)) { echo '<tr>'; foreach($row as $field) { echo '<td>' . htmlspecialchars($field) . '</td>'; } echo '</tr>';} Quote Link to comment Share on other sites More sharing options...
benanamen Posted January 1, 2016 Share Posted January 1, 2016 @Ani123, No, no, no! You dont loop through the data twice. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.