PHP函数并在html中调用它

I'm very new to PHP, so please excuse me if this is a silly question.

I have a separate PHP file which contains a few functions, they were originally in the same document as the HTML site, but I wanted to move them to their own PHP file.

Originally in the HTML I had PHP connect to an SQL server and then ran an SQL Query, then I wanted the results of that query to populate a drop down box, so each row in the returned query was an , this worked great but when I move all that functionality to the separate PHP file it's no longer working.

I have one function now, which connects to the database, runs the query and then attempts to write the output, but I think it's this section that's causing the problem.

I have included this new PHP function file.

Here's the function:

function results() {
    $connection = db();

    $smt = $connection->prepare('SELECT * FROM tblResults');
    $smt->execute();
    $data = $smt->fetchAll();

    foreach ($data as $row) {
    echo "<option>".$row['Score']."</option>";
    }
}

And in the HTML I have this code to call the function:

<select class="selectpicker" data-live-search="true" data-width="auto">
    <?php results(); ?>
</select>

As I said, I'm new to PHP so I'm sorry if this is an obvious error or if the format is wrong.

Any help would be greatly appreciated.

I'm not sure what the issue was, but I've managed to fix it.

One of the functions was a connection to the database and it had a session_start(); within it, and although I was including this function php file at the beginning, it looks like I have to also put in a session_start(); at the beginning of my function results(); so it now looks like this.

function results() {
session_start();

//databse connection Sting
$connection = new PDO("sqlsrv:server=localhost;Database=Results", "user", "password"); 

$smt = $connection->prepare('SELECT * FROM tblResults');
$smt->execute();
$data = $smt->fetchAll();

foreach ($data as $row) {
echo "<option>".$row['Score']."</option>";
}

}

Again, I'm not sure why I need to establish the session_start but it seems to have solved the issue.

Thank you for all your responses, you helped me break the issue down.

Cheers!

You cant call the function in this way... You need to include the php file first and then you can call the function if its not included in the class, else you need to create an object to call the function.

first you have to include function file.
try this

<select class="selectpicker" data-live-search="true" data-width="auto">
<?php include("******.php"); ?>
<?php results(); ?>

You need to include the PHP file in the file with HTML code.

<?php
include("fileName.php");
?>

Use following code

     $smt = $connection->prepare('SELECT * FROM tblResults');
     $smt->execute();
     $data = $smt->fetchAll();     
     <select class="selectpicker" data-live-search="true" data-width="auto">
       <?php
          foreach ($data as $row) {
       ?>
              <option><?php echo $row['Score'] ?></option>;
              <?php } ?>
     </select>

put the above function in php file suppose resultfile.php

<?php
function results() {
    $connection = db();

    $smt = $connection->prepare('SELECT * FROM tblResults');
    $smt->execute();
    $data = $smt->fetchAll();

    foreach ($data as $row) {
    echo "<option>".$row['Score']."</option>";
    }
}
?>

change your index.html extention to index.php and add below line at the beginnig

so you should have two php file ,one with php function and another with your html code and include php .

calling the index.php should generate result if db () is properly configured . Try with simple echo if your are new