500错误尝试使用PHP脚本创建函数时

时间:2016-04-26 10:49:31

标签: php mysql function

我正在尝试创建一个在数据库中搜索表的函数。我有一个搜索脚本。我想将此脚本用作函数。

这是我的剧本。

<?php
if(isset($_GET['q'])){
    error_reporting(-1);

    $query = $_GET['q'];

    function search_aya($query) {

        include 'core/db.inc.php';

        $dbh  = new mysqli($host, $user, $password,  $database);

        if ($dbh->connect_error) {
            echo 'Unable to connect to database '. $dbh->connect_error;
        } else {



            if ($stmt = $dbh->prepare("SELECT id, sura, aya, text FROM bn_bengali WHERE MATCH(text) AGAINST(?) "))

                {
                $stmt->bind_param("s", $query);
                // executing the statement
                $stmt->execute();
                // binding the table columns Name and Email to the $Name and Email parameters respectively.
                $stmt->bind_result($id, $sura, $aya, $text);
                /* fetch associative array */

                return $stmt;


            } else {
                    echo "Prepare failed: (" . $dbh->errno . ") " . $dbh->error;
            }
        }

    } //ending function

    $stmt = search_aya($query);
    while ($stmt->fetch()) {
        echo $sura.'-'.$aya;
        echo $text;
        echo '<hr />';
    } 

} // end isset get q
else
{
     echo '<form action="" ><input type="text" name="q"><button>Search</button></form>';
}
?>

错误地说&#34; 500 - 内部服务器错误&#34;

你能告诉我使用这个脚本制作函数的正确方法吗?

在日志文件中,我发现了类似的内容,

我认为此错误的日志是

2016-04-26 11:19:15: (src/mod_fastcgi.c.3346) response not received, request sent: 990 on socket: tcp:127.0.0.1:9998 for /quran/search_func_array_ret.php?q=hi, closing connection 

2016-04-26 11:21:31:(src / mod_fastcgi.c.2562)意外的文件结束(也许fastcgi进程死了):pid:15303 socket:tcp:127.0.0.1:9998 2016-04-26 11:21:31:(src / mod_fastcgi.c.3303)孩子发信号:11

完整的日志文件。

[26-Apr-2016 17:27:24 Asia/Jakarta] PHP Catchable fatal error:  Object of class mysqli_stmt could not be converted to string in /storage/emulated/legacy/pws/www/quran/search_func_array_ret.php on line 45
[26-Apr-2016 17:32:57 Asia/Jakarta] PHP Notice:  Use of undefined constant query - assumed 'query' in /storage/emulated/legacy/pws/www/quran/search_func_array_ret.php on line 66
[26-Apr-2016 17:32:57 Asia/Jakarta] PHP Notice:  Undefined variable: host in /storage/emulated/legacy/pws/www/quran/search_func_array_ret.php on line 30
[26-Apr-2016 17:32:57 Asia/Jakarta] PHP Notice:  Undefined variable: user in /storage/emulated/legacy/pws/www/quran/search_func_array_ret.php on line 30
[26-Apr-2016 17:32:57 Asia/Jakarta] PHP Notice:  Undefined variable: password in /storage/emulated/legacy/pws/www/quran/search_func_array_ret.php on line 30
[26-Apr-2016 17:32:57 Asia/Jakarta] PHP Notice:  Undefined variable: database in /storage/emulated/legacy/pws/www/quran/search_func_array_ret.php on line 30
[26-Apr-2016 17:32:57 Asia/Jakarta] PHP Fatal error:  Call to a member function fetch() on a non-object in /storage/emulated/legacy/pws/www/quran/search_func_array_ret.php on line 67
[26-Apr-2016 17:33:30 Asia/Jakarta] PHP Notice:  Undefined variable: host in /storage/emulated/legacy/pws/www/quran/search_func_array_ret.php on line 30
[26-Apr-2016 17:33:30 Asia/Jakarta] PHP Notice:  Undefined variable: user in /storage/emulated/legacy/pws/www/quran/search_func_array_ret.php on line 30
[26-Apr-2016 17:33:30 Asia/Jakarta] PHP Notice:  Undefined variable: password in /storage/emulated/legacy/pws/www/quran/search_func_array_ret.php on line 30
[26-Apr-2016 17:33:30 Asia/Jakarta] PHP Notice:  Undefined variable: database in /storage/emulated/legacy/pws/www/quran/search_func_array_ret.php on line 30
[26-Apr-2016 17:33:30 Asia/Jakarta] PHP Fatal error:  Call to a member function fetch() on a non-object in /storage/emulated/legacy/pws/www/quran/search_func_array_ret.php on line 67

但我不知道为什么,这段代码工作正常,

    <?php

 if (isset($_GET['q'])){

       // error_reporting(-1);

     //  ini_set('display_errors', 1); 
      // ini_set('display_startup_errors', 1); 
    //   error_reporting(E_ALL);






      $query = $_GET['q'];





   function search_aya($query) {

       require 'core/db.inc.php';

        $dbh  = new mysqli($host, $user, $password,  $database);

        if ($dbh->connect_error) {
            echo 'Unable to connect to database '. $dbh->connect_error;
        } else {



            if ($stmt = $dbh->prepare("SELECT id, sura, aya, text FROM bn_bengali WHERE MATCH(text) AGAINST(?) "))

               {
                $stmt->bind_param("s", $query);
                // executing the statement
                $stmt->execute();
                // binding the table columns Name and Email to the $Name and Email parameters respectively.
                $stmt->bind_result($id, $sura, $aya, $text);
                /* fetch associative array */



                while ($stmt->fetch()) {
                    echo $sura.'-'.$aya;
                    echo $text;
                    echo '<hr />';
                    } 


            } else {
                   echo "Prepare failed: (" . $dbh->errno . ") " . $dbh->error;
            }
        }

     } //ending function



        search_aya($query);

      /*  $stmt = search_aya($query);
        while ($stmt->fetch()) {
                    echo $sura.'-'.$aya;
                    echo $text;
                    echo '<hr />';
                    } */









} // end isset get q
else
{
     echo '<form action="" ><input type="text" name="q"><button>Search</button></form>';
}
    ?>

1 个答案:

答案 0 :(得分:1)

1)您似乎正在使用Palapa Web Server。要确保获得实际的错误消息,请打开应用程序中的菜单,然后转到“Web服务器设置”=&gt; “PHP设置”并确保它的配置如下:

enter image description here

2)从你发布的日志中,看起来你正在使用几个未定义的变量。我想应该在文件core/db.inc.php中定义你include - 。

  • include更改为require。这样,当文件不存在时,PHP将抛出错误。
  • 确保文件确实存在。
  • 如果文件确实存在,它可能存在于与PHP正在寻找的路径不同的路径中。 PHP总是解析相对于当前目录include / require语句(通常是原始脚本所在的文件夹,即您在URL中调用的文件夹),不是当前解析文件所在的文件。

例如,您可能有http://localhost:8000/test.php之类的网址,其中包含/storage/emulated/legacy/pws/www/test.php。然后,此文件使用include('quran/search_func_array_ret.php');。然后PHP将加载/storage/emulated/legacy/pws/www/quran/search_func_array_ret.php

但是,现在search_func_array_ret.php使用include('core/db.inc.php');,PHP不会查找/storage/emulated/legacy/pws/www/quran/core/db.inc.php,而是查找/storage/emulated/legacy/pws/www/core/db.inc.php

您可以通过在脚本顶部临时编写die(getcwd());来测试此理论 - 然后您将看到与include / require语句相关的路径。< / p>

如果这是问题所在,您需要修改include / require中使用的路径(提示:您可以使用__DIR__来引用当前已解析的目录文件位于,例如require(__DIR__ . '/core/db.inc.php');)或将文件移动到正确的位置。

3)我不确定bind_result是否可以跨越函数边界,就像你现在正在做的那样。如果您发现$sura$aya$text为空,则需要将$stmt->bind_result($id, $sura, $aya, $text);移出while($stmt->fetch())语句上方的函数。< / p>

但是,更好的方法是在函数中使用return $stmt->get_result()->fetch_all(MYSQLI_ASSOC);,然后迭代结果如下:

$results = search_aya($query);
foreach($results as $row) {
    echo $row['sura'] . '-' . $row['aya'];
    echo $row['text'];
    echo '<hr />';
}

编辑:如果您的PHP版本早于5.3.0,则无法使用fetch_all。相反,在函数中使用此代码:

$sqlResult = $stmt->get_result();
$results = array();
while($row = $sqlResult->fetch_assoc()) $results[] = $row;
return $results;

EDIT2 :实际上,既然fetch_all的优势已经消失了,您还可以返回$stmt->get_result()并将您的循环更改为:

$sqlResult = search_aya($query);
foreach($row = $sqlResult->fetch_assoc()) {
    echo $row['sura'] . '-' . $row['aya'];
    echo $row['text'];
    echo '<hr />';
}

然后,您可以删除对您函数内bind_param的调用。

EDIT3 我刚刚意识到我之前犯了一个错误,我忘了必须首先在语句上调用get_result来接收结果对象,然后允许调用fetch_allfetch_assoc。我相应地纠正了答案。

请注意,get_result以及之后fetch_allfetch_assoc仅在PHP可以使用本机SQL驱动程序时才有效。如果它不起作用,您需要继续使用旧方法,但只需将行$stmt->bind_result($id, $sura, $aya, $text);移到函数外部。