Search code examples
phpfilerequireclassnotfound

Require/Include in php


I need some help, I have 2 php files and when I am calling some functions it tells me that

Fatal error: Call to undefined function get_contents() in /home/f77inq/public_html/php/shares.php on line 13

eventhough in the files from where I call the functions I have typed require("name ofmy file");

shares.php

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'> 
<html xmlns='http://www.w3.org/1999/xhtml'> 
<head> 
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> 
<title>Shares</title> 
<link rel='stylesheet' type='text/css' href='style.css' /> 
</head> 

<body> 
adad
<?php

    $data = get_contents();
    print_contents($data);
?>
</body> 

</html> 

This is the file from where I am calling the functions.

    <?php

require("../../php_include/services.php");
require("../../php_include/query.php");


putenv("ORACLE_HOME=/oracle/product/12.1.0/dbhome_1");


function get_contents() {

    $conn = db_connect();
    $shares = get_share($conn);
    db_disconnect($conn);
    $contents = array('shares' => $shares);
    return $contents;
}


function print_contents($contents) {
    ?>
        <table>
            <tr>
                <th>Company Name</th>
                <th>Rate</th>

            </tr>
    <?php
    foreach ($contents['shares'] as $share) {
        print "<tr>";

    //****** LINKS TO EVERY
        $identifier = urlencode($share['COMPANY']);
        print "<td><a href='share-details.php?company={$identifier}'>{$share['COMPANY']}</a></td>";

        print "<td>{$share['RATE']}</td>";

        print "</tr>";
    }
    ?>
        </table>
    <?php
}
require ("shares.php");            //<<<<<<<<<<<<<<<<<<<<<<
?>

As you can see down bellow I require the shares php file.

Both files are located in the same folder


Solution

  • for testing i put this code in a file named like.php-

    <?php
    function get_contents() {
            echo "Got it";
    }
    require ("shares.php");
    ?>
    

    and in the shares.php file i used this-

    <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'> 
    <html xmlns='http://www.w3.org/1999/xhtml'> 
    <head> 
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> 
    <title>Shares</title> 
    <link rel='stylesheet' type='text/css' href='style.css' /> 
    </head> 
    
    <body> 
    <?php
    
        $data = get_contents();
         echo $data;
    ?>
    </body> 
    
    </html>
    

    it works perfectly without eny error.