PHP代码在HTML页面显示,无法从数据库获取数据求助
Hey there! Let's work through why your PHP code is displaying in the browser instead of loading your database data. This is a super common hiccup for new PHP developers, so don’t stress—we’ll get this sorted step by step.
First off, this issue almost always boils down to your server not properly parsing and executing the PHP code (database connection problems might come later, but let’s fix the code display issue first). Here are the most common fixes to check:
1. Double-Check Your File Extension
- Make sure your file ends with
.php, not.html,.txt, or anything else. If your file is named something likeindex.html, even with PHP inside, your server will treat it as a static HTML file and won’t run the PHP code.
2. Verify Your Local Server Is Running & Files Are in the Right Place
- If you’re using an integrated environment like XAMPP, WAMP, or MAMP, confirm the Apache (or Nginx) service is active. Open your control panel and make sure Apache shows as "Running".
- Also, ensure your PHP file is saved in the server’s root directory: for XAMPP it’s
htdocs, for WAMP it’swww, and for MAMP it’shtdocstoo. Files outside this folder won’t be processed by the server.
3. Confirm PHP Is Properly Installed/Configured
- If you set up your server manually (not using an integrated tool), you need to make sure Apache/Nginx is configured to parse PHP. For Apache, check your config file for lines like these (adjust paths to match your PHP version):
LoadModule php8_module "C:/php/php8apache2_4.dll" AddHandler application/x-httpd-php .php PHPIniDir "C:/php" - Test if PHP is working by creating a simple
test.phpfile with this code:
Visit<?php phpinfo(); ?>localhost/test.php—if this page also shows raw PHP code, your server isn’t set up to handle PHP at all. Reinstall your integrated environment or fix the server config.
4. Fix Incomplete Code Syntax
Looking at your code snippet, the final header <th class="CelluleTab14... is cut off, which might mean you have unclosed HTML tags or incomplete PHP logic. Syntax errors can break the server’s ability to parse the entire PHP block, leading to partial code being displayed.
Here’s a cleaned-up, complete version of your code to use as a reference (make sure to fill in missing parts and confirm your database connection is valid):
<?php // First, ensure your database connection is properly initialized (example for PDO): // $connexion = new PDO('mysql:host=localhost;dbname=your_db_name;charset=utf8', 'your_username', 'your_password'); $reponse = $connexion->query('SELECT * FROM projet_sp'); ?> <table> <tr> <th class="CelluleTab11"> Num dossier </th> <th class="CelluleTab12"> Nom projet </th> <th class="CelluleTab13"> DAS </th> <th class="CelluleTab14"> [Your Missing Header Text] </th> </tr> <?php // Loop through the query results to display data while ($donnees = $reponse->fetch()) { ?> <tr> <td><?php echo $donnees['num_dossier']; ?></td> <td><?php echo $donnees['nom_projet']; ?></td> <td><?php echo $donnees['das']; ?></td> <td><?php echo $donnees['your_field_name']; ?></td> </tr> <?php } ?> </table>
5. Clear Your Browser Cache
Sometimes browsers cache old, broken versions of your page. Press Ctrl+F5 (or Cmd+Shift+R on Mac) to force a hard refresh, or clear your browser’s cache entirely before reloading.
Once you’ve fixed the PHP parsing issue, if you still don’t see database data, check that your $connexion variable is correctly set up (no connection errors), your SQL query is targeting the right table/columns, and there’s actually data in the projet_sp table. You can add var_dump($reponse); after the query to see if it returns any results.
内容的提问来源于stack exchange,提问作者Gautier Martin

