Generating an HTML table from SQL data using PHP

I'm attempting to create a dynamic HTML table using PHP to populate it with data from a MySQL database. I've tried using a while loop, but the issue is that it ends up displaying the same first row multiple times.

<div class = "container">
        <p>Registered companies:</p>
        <table border = "1px" align = "left">
            <tr>
                <th>Username</th>
                <th>Company name</th>
                <th>Company value1</th>
                <th>Company value2</th>
            </tr>
            <?php
                $compRowIncrement = 0;
                while ($compRowIncrement < $companyRowCount) {
            ?>
                    <tr>
                        <td><?php echo $companyRow['user_name']?></td>
                        <td><?php echo $companyRow['company_name']?></td>
                        <td><?php echo $companyRow['company_value1']?></td>
                        <td><?php echo $companyRow['company_value2']?></td>
                    </tr>
            <?php
                    $compRowIncrement++;
                }
            ?>
        </table>
    </div>

Currently, the table should display 3 rows of data based on SQL query results. However, it's only displaying 3 rows with the same data (repeating the first value obtained from the database).

How can I ensure that each table row is populated with different data from the database?

I'm still learning, so please overlook any CSS values in the table :)

EDIT1 (Added query)//

$getPlayerCompanies = $MySQLi_CON -> query("SELECT DISTINCT *
                                              FROM companies
                                              LEFT JOIN player ON companies.player_id = player.player_id
                                              LEFT JOIN users ON users.user_id = player.user_id
                                              WHERE users.user_id =".$_SESSION['userSession']);
$companyRow = $getPlayerCompanies -> fetch_array();
$companyRowCount = $getPlayerCompanies -> num_rows;

The current query fetches and returns 3 rows as intended.

Answer №1

Where exactly are the $companyRow values coming from?

I believe your code should look more like this (or consider using mysqli commands)

<?php
     while ($companyRow = $getPlayerCompanies -> fetch_array() ) 
     {
?>
         <tr>
             <td><?php echo $companyRow['user_name']?></td>
             ...
         </tr>
<?php 
     }
?>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to utilize silex's "finish" middleware for efficiently handling resource-intensive tasks in the background?

Currently, I am creating an app using Silex and following the documentation with some additional features. I have set up the route, added middleware for the route, and included finish middleware for the app. $app->put('/request/', function (R ...

Steps to establish a connection between PHP and Microsoft SQL Server

I am currently using a dedicated CentOS server that needs to establish a connection with a remote Windows machine running SQL Server 2012. The provider has installed Freetds, and my PHP version is 5.3.28. We are able to ping the remote MS-SQL server from ...

Getting an Error in Symfony2 "Issue with spl_object_hash() function - parameter not matching"

Whenever I attempt to save my object and execute it, a noticeable error crops up: Warning: spl_object_hash() expects parameter 1 to be an object, but instead integer was provided. This results in a 500 Internal Server Error - ContextErrorException I un ...

Can you explain the relationship between HTML 5 Canvas coordinates and browser pixels?

When trying to use HTML 5 canvas as a drawing board in my application, I encountered an issue. The drawings appear at an offset from the mouse pointer. My method involves using the Jquery .offset function to determine the event's position on the canv ...

Implementing websocket functionality in Yii framework

I am currently working on integrating websocket functionality into my web application using PHP and as an extension for Yii. This will allow me to create a notification system similar to what I have in mind. My starting point is the code available at the ...

Content and visual side by side

My logo and header image are giving me trouble. Whenever I attempt to center the header image, it keeps moving to the next row. How can I make sure it stays on the same row? Check out our website Thank you ...

Using the limit function in ajax-accordion's page when invoking JPagination

Hello everyone! I recently faced an issue with my Joomla website. I am using a Joomla template and in the 'views' directory, there is a page that displays a list of employees using an 'ajax-accordion'. Each employee's name serves ...

Working with newline-separated JSON in PHP

Is there a way to achieve this using PHP? I am having trouble decoding it and keep getting null values. Can anyone provide an example of how to separate each line and decode them? Check out the JSON file here: Here is some code that I have added: $jsonF ...

Automated client connection negotiation for memcached's latest feature

My attempts to utilize memcached to store PHP session data have hit a snag. PHP keeps generating an error message indicating it can't establish a connection: Warning: session_start(): open(memcached:11211/sess_hitr4obt9ofmmsvk9kfl8euqt6, O_RDWR) f ...

PHP version 7.2.1 is throwing an error indicating an undefined index for the file_upload

Encountering an error message: Notice: Undefined index: file_upload in C:\MAMP\htdocs\basic_files\upload.php on line 3 Each time I attempt to upload a file using the form. While many attribute this issue to problems with enctype or ...

The edges of the cube are not joined together

Looking for a 3D transform cube that can freely flip left and right, with the button fitting into it perfectly. I've found the CSS code to achieve this, but when I set the width of the ".cube" below 200px, the borders of the cube don't connect. I ...

Why won't my Laravel AJAX request work after refreshing the page?

In my Laravel CRUD web application, users can view and edit records with a sorting feature that asynchronously sorts the main records view using AJAX. Everything functions smoothly until a user clicks on a record to edit it. After being redirected through ...

Pattern in PHP to extract strings similar to [3,4,aAn] using regular expressions

Looking to extract values such as [3,6,AN] from a given string using the code snippet below: $custom='[4,100,A]@erric.com'; $regexIpAddress = '/\[\d{1,3}\,\d{1,3}\,[a|A]\]/'; preg_match($regexIpA ...

Displaying dropdown options based on the previous selection made by the user

Can I link the data in my second dropdown to the selection made in the first dropdown? I tried a similar solution from stackoverflow without success. You can find the reference here. The code works when directly copied and pasted but not within my system. ...

Navigating through pages: How can I retrieve the current page value for implementing next and previous functions in Angular 7?

Greetings, I am a new learner of Angular and currently working on custom pagination. However, I am facing difficulty in finding the current page for implementing the next and previous functions. Can anyone guide me on how to obtain the current page value? ...

"Can someone guide me on the process of transmitting data to a client using Node in combination with

I am new to web development and struggling to understand how to transfer data from the Node server to the client while also displaying an HTML page. I am aware that res.send() is used to send data, but I'm having difficulty maintaining the client disp ...

CSS animation for input range slider

Is there a way to create smoother animations for the input[type="range"] element, especially when dealing with short audio files? You can check out my Codepen where I've been experimenting with solutions using a short audio file: Codepen Link I am s ...

Can you explain the concept of the repeater argument used in pack?

I'm looking to transform a string into hexadecimal format. I've come across the pack function as a potential solution. Can someone clarify the distinction between the following use cases? $hexRepresentation = pack('H','I will be c ...

The functionality of $GLOBALS['TSFE']->set_no_cache() seems to be ineffective starting from TYPO3 version 6.2.17 and newer versions

I previously implemented the 'set_no_cache' function globally in the initialize action of my extension. $GLOBALS['TSFE']->set_no_cache(); However, it seems that this method is not effective starting from typo3 version 6.2.17 onward ...

What is the method for turning off Bootstrap for viewport width below a specific threshold?

I'm currently utilizing bootstrap for my website design. However, there's a particular CSS rule causing some frustration on one specific page: @media (min-width: 576px) .col-sm-4 { -ms-flex: 0 0 33.333333%; flex: 0 0 33.333333%; max-w ...