0

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.

Cal Brown
  • 145
  • 1
  • 1
  • 8
  • Can you update your post and tell us the error plz ? – zenko Feb 07 '17 at 11:11
  • did you include the php page in html? – affaz Feb 07 '17 at 11:11
  • `.html` files can't normally execute PHP. Either rename the second file to`.php`, or configure your webserver so that it processes `.html` files with PHP. Then you need to put `` into the second file. – Barmar Feb 07 '17 at 11:14
  • I've found a post that might be useful for you [How to run a PHP function in html](http://stackoverflow.com/questions/5968280/how-to-run-a-php-function-from-an-html-form) –  Feb 07 '17 at 11:14
  • Sorry, I should have pointed out that I have included the PHP function file and that both files are saved as PHP. It still doesn't work. – Cal Brown Feb 07 '17 at 11:15
  • what kind of error have you when you run it ? or you don't have error ? – Frankich Feb 07 '17 at 11:17
  • I'm not shown any error, it creates the drop down box but then just stops. When checking the source code of the page it looks like it's just cut off when it hits the PHP script. – Cal Brown Feb 07 '17 at 11:23
  • Can you show us your html / php code as it is written ? (With Is the function db() declared in the same file as results() ? Does it return a PDO instance (seems to)... ? – zenko Feb 07 '17 at 11:44
  • debugging: at the top of the PHP file, immediately before the `include 'utils.php';` [put the settings from this answer](http://stackoverflow.com/a/21429652/3184785). You should now see any errors when you run the code. It should help you to find out what is not working as it should. – Ryan Vincent Feb 07 '17 at 11:56

6 Answers6

0

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.

Ankit Jain
  • 898
  • 7
  • 12
0

first you have to include function file.
try this

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

Prodip Das
  • 31
  • 3
0

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

<?php
include("fileName.php");
?>
Abhay
  • 314
  • 1
  • 2
  • 11
0

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>
ChandraShekar
  • 386
  • 5
  • 12
  • That's what I had previously when I was just using one file, but I would like a separate functions PHP file, so I can't see how I would implement the above code within the function as it contains HTML. – Cal Brown Feb 07 '17 at 11:22
0

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!

Cal Brown
  • 145
  • 1
  • 1
  • 8
0

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