【问题标题】:Require/Include in php [closed]需要/包含在php中[关闭]
【发布时间】:2015-04-24 19:02:02
【问题描述】:

我需要一些帮助,我有 2 个 php 文件,当我调用一些函数时它告诉我

致命错误:在第 13 行调用 /home/f77inq/public_html/php/shares.php 中未定义的函数 get_contents()

即使在我调用我输入的函数的文件中 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> 

这是我从中调用函数的文件。

    <?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");            //<<<<<<<<<<<<<<<<<<<<<<
?>

正如您在下面看到的,我需要共享 php 文件。

两个文件位于同一个文件夹中

【问题讨论】:

  • 您是否在 share.php 文件中包含了 php 文件??
  • 这似乎倒退了。通常你把函数放在你用require加载的脚本中调用函数的脚本中。
  • @habibulhaq 他这样做是相反的:定义函数的脚本包括share.php。转到第二个脚本的末尾。
  • @KhairulIslam 这没有意义。
  • @Nico12345,您这里的代码绝对有效并且工作正常。还有一些你不知道的事情发生了,在文件顶部运行 debug_print_backtrace() 并查看它的加载位置。

标签: php file require classnotfound


【解决方案1】:

为了测试,我把这段代码放在一个名为 like.php-的文件中-

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

在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> 
<?php

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

</html>

完美运行,没有任何错误。

【讨论】:

  • 对我来说这仍然是这个可怕的错误((
  • @Nico12345,您需要弄清楚如何向我们提供出错的真实代码。此代码无效。
  • @KorvinSzanto 我和 Khairul 做了同样的事情,我遇到了同样的问题。
  • 他不知道自己在说什么。他的建议毫无意义。
  • 等待,等待@Nico12345 让我们在浏览器中知道您正在运行的网址。类似于localhost/like.phplocalhost/shares.php
【解决方案2】:

您显示的错误来自文件shares.php,而您require 的文件是share.php。由于您显示的所有代码均有效且确实有效,因此此错误很可能发生在流程的其他地方。

【讨论】:

  • 实际上是共享,我又错字了))。看起来它与数据库有关,或者我不知道,因为我做得对(
  • @Nico12345 您粘贴的错误显示“shares.php”,这不是由数据库问题或类似问题引起的。您提供的信息不正确。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-15
  • 2012-04-03
  • 2015-09-22
  • 1970-01-01
相关资源
最近更新 更多