1

I've a class project that I am working on that I would like to add database functionality. My PHP skills are less than par and my deadline is short. I have a database I used elsewhere that I would like to use again but I ran into an issue. I have the HTML done for the site but have no idea how to use my DB with it. The old one ran from index.php and my new page is in html.Is it possible to grab the old file and use it inside my html like with MVC views or just add it to the page itself? I don't mind doing the research but I am not really sure what I am looking for.

-Thanks.

4
  • with usual php setup, html files are not interpreted by php. Commented Apr 14, 2014 at 4:47
  • You can include PHP files inside HTML file.The good practice is write the php file for database in a seperate file and include this in the php file Commented Apr 14, 2014 at 4:48
  • You can't include php files in .html files. If the page uses php includes, it can't have an .html extension. However, you can always include php files in php files, in addition to js/css/html. Commented Apr 14, 2014 at 4:49
  • Then I am lucky because my php file from a previous assignment is in it's own file. What is the connection called so I can research that? Commented Apr 14, 2014 at 4:50

2 Answers 2

2

PHP generates HTML and not the other way around.

As a quick solution I would rename the .html files to .php and use the existing html with the php heredoc function, ex:

<?php

echo <<< EOF

your html code here

EOF;

?>

if you need to connect to a database, you can append the db output to the existing html, like this:

<?php

$db = new mysqli('localhost', 'user', 'pass', 'demo');

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}
    
echo <<< EOF
first part of the html here
EOF;

//Queries the DB and outputs the results of all **employees**
$result = $db->query("SELECT id, name, salary FROM employees");
while (list($id, $name, $salary) = $result->fetch(PDO::FETCH_NUM)) {
echo <<< EOF
 <tr>
          <td><a href="info.php?id=$id">$name</a></td>
          <td>$salary</td>
          </tr>
EOF;
}

echo <<< EOF
second part of the html here
EOF;

//and so on...


?>

Make sure you run the code on a server that supports php.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I actually just figured the renaming thing out myself but didn't know about the heredoc.
I prefer the heredoc to echo, this way I don't need to escape double quotes, etc...
1

According what I think It's all about code readability. But this could be vary depending on the situation. This has been already answered.

Escape HTML to PHP or Use Echo? Which is better?

If you have any questions regrading this don't hesitate to make a comment.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.