如何在HTML文件中混合PHP [关闭]

When I use iframe in HTML file that linked to test.php .. the result is OK

Test.php :

<?php     
   $host = "localhost";
   $user = "user";
   $pass = "pass";
   $database = "db";
   $koneksi = mysql_connect ($host,$user,$pass);

   mysql_select_db($database,$koneksi) or die ("error");

   $result = mysql_query("SELECT * FROM pengumuman"); 
   $i = 0;

   while($row = mysql_fetch_array($result)) { 
        echo "<li><a href='#'>".$row['judul']."</a></li>";
        $i++;
   } 

   mysql_close($koneksi); 
?>

But I do want to separate this test.php, so I mixed it in my HTML file

<?php 
   $host = "localhost";
   $user = "user";
   $pass = "pass";
   $database = "db";
   $koneksi = mysql_connect ($host,$user,$pass);

   mysql_select_db($database,$koneksi) or die ("error");

   $result = mysql_query("SELECT * FROM pengumuman"); 
   $i = 0;

   while($row = mysql_fetch_array($result)) { 
?> 
   <li><a href='#'><?php $row['judul'] ?> </a></li>

<?php 
        $i++;
   }   
    mysql_close($koneksi); 
?>

And the result not OK anymore! Truly, I'm new in web prog .. Can anyone help me ?

This is not possible:

<?php
   while($row = mysql_fetch_array($result)) { 
?> 
   <li><a href='#'><?php $row['judul'] ?> </a></li>

<?php 
        $i++;
   }
?>

But you can use the code below instead that:

<?php
$host = "localhost";
$user = "user";
$pass = "pass";
$database = "db";
$koneksi = mysql_connect($host,$user,$pass);

mysql_select_db($database,$koneksi) or die("error");

$result = mysql_query("SELECT * FROM pengumuman");
$i = 0;
?>

<!--
Other HTML Codes Here
//-->

<?php
while($row = mysql_fetch_array($result)) {
    echo "<li><a href='#'>" . $row['judul'] . " </a></li>";
    $i++;
}
?>

<!--
Another HTML Codes Here
//-->

<?php mysql_close($koneksi); ?>

But, I didn't check your PHP if it's right, for I'm chillin'.


I think this is want you want!
Create open.php:

<?php
$host = "localhost";
$user = "user";
$pass = "pass";
$database = "db";
$koneksi = mysql_connect($host,$user,$pass);

mysql_select_db($database,$koneksi) or die("error");

$result = mysql_query("SELECT * FROM pengumuman");
?>

Create close.php:

<?php mysql_close($koneksi); ?>

And the basename of your web page that included a PHP code must have a .php file extension, such like div.php because commonly, a PHP file must have a .php extension, but sometimes a web host server configured to allow a HTML file to read a PHP code, but it isn't normally. Let say your so called <div> HTML is in the div.php file:

<?php include 'open.php'; ?>
<div>
  <ol>
<?php
$i = 0;

while($row = mysql_fetch_array($result)) {
    echo "    <li><a href='#'>" . $row['judul'] . "</a></li>";
    $i++;
}
?>
  </ol>
</div>
<?php include 'close.php'; ?>

you probably meant to add an echo before $row['judul']

<?php echo $row['judul']; ?>

simply putting the variable will not output it. Maybe you were thinking of short notation that is disabled by default

There are two things you have to take care in this case.

1) Add an echo before $row['judul']

<?php echo $row['judul']; ?>

2) Make sure that the extension of your file is '.php', if your file is test.html then change it to test.php and try.

NB: If you don't want to change your file extension, you must change some server configurations for handling files with .html files. In apache you can achieve this by adding 'AddHandler' in httpd.conf file.