1

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 ?

TNK
  • 4,263
  • 15
  • 58
  • 81
matzone
  • 5,703
  • 3
  • 17
  • 20
  • 2
    [Please, don't use mysql_* functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) in new code. They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [_prepared statements_](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – TNK Apr 02 '13 at 02:39
  • 1
    you can not mix PHP in HTML file, but you can mix HTML in PHP file. – anru Apr 02 '13 at 02:45
  • @metal_fan Ups I miss that .. But anyway there still no result in my HTML
    – matzone Apr 02 '13 at 03:07
  • Also, not sure why you're incrementing $i. You're not using it for anything. – user1048676 Apr 02 '13 at 03:07
  • @user2234261 Are you sure that $row['judul'] has a value? – user1048676 Apr 02 '13 at 03:08
  • @user1048676: $i not used yet .. I'll have it for item numbering .. – matzone Apr 02 '13 at 03:17

3 Answers3

2

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'; ?>
  • What do you mean, to input SQL, PHP `$_POST` Function, `` tag or what? You can even write a HTML tag in PHP. Or it's this, `$select = "SELECT * FROM pengumuman"; $result = mysql_query("$select"); echo $select;` Sorry I didn't get what's in your mind. – 5ervant - techintel.github.io Apr 02 '13 at 12:17
  • I have MySQL data. Via test.php I can display the data. And it's work too when I call test.php in my HTML iFrame. But .. I want the result in my
    HTML file .. that's all
    – matzone Apr 03 '13 at 03:34
  • Is this what you've looking for: `` if you want to include `test.php` or `` to require it. You can even change the `include` or `require` to `include_once` or `require_once` if you want the specific file to be included/required once to a specific webpage. – 5ervant - techintel.github.io Apr 03 '13 at 03:56
  • Such `
    ` or you can even style `test.php` before including it?
    – 5ervant - techintel.github.io Apr 03 '13 at 04:23
  • I think the best thing you could do is to seperate the opening from the closing, such what's added on the answer. – 5ervant - techintel.github.io Apr 03 '13 at 05:03
  • .. No, the result is empty space .. – matzone Apr 03 '13 at 05:28
  • Sorry, your next code was not worked for me .. There's only " . $row['judul'] . ""; $i++;}?> " in my
    box .. but thx anyway ..
    – matzone Apr 03 '13 at 06:01
  • Ah.. I see! Seems like your so called `div.html` couldn't read a PHP code, 'cause the server's not configured a HTML file to read a PHP code. The solution is to change the file extension into `.php` so your so called `div.html` will become `div.php` because there's a PHP code. I'll edited the answer after this. – 5ervant - techintel.github.io Apr 03 '13 at 06:12
  • You could even rename your file with `.phtml` file extension. For it's one of the specific extensions for a PHP file. – 5ervant - techintel.github.io Apr 03 '13 at 06:41
  • 1
    Hey thanks .. for your .phtml info ! My page displayed as I need !! – matzone Apr 03 '13 at 07:00
  • Gladly.. Maybe you could choose this thread as an answer to your issue, only if it satisfied you. – 5ervant - techintel.github.io Apr 03 '13 at 07:14