尝试使用PHP脚本生成函数时出错500

尝试使用PHP脚本生成函数时出错500,php,mysql,function,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, $passwo

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

这是我的剧本

<?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>';
}
?>
2016-04-26 11:21:31:(src/mod_fastcgi.c.2562)文件意外结束(可能是fastcgi进程死机):pid:15303套接字: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)您似乎正在使用Palapa Web服务器。要确保收到实际错误消息,请打开应用程序中的菜单,然后转到“Web服务器设置”=>“PHP设置”,并确保其配置如下:

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

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

  • include
    更改为
    require
    。这样,当文件不存在时,PHP将抛出一个错误
  • 确保文件确实存在
  • 如果文件确实存在,它可能存在于不同于PHP查找它的路径中。PHP总是解析与当前目录(通常是原始脚本所在的文件夹,即在URL中调用的文件夹)相关的
    include
    /
    require
    语句,而不是当前解析文件所在的文件
例如,您可能有一个类似
http://localhost:8000/test.php
运行
/storage/emulated/legacy/pws/www/test.php
。然后,该文件使用
include('quoran/search\u func\u array\u ret.php')。然后PHP将加载
/storage/emulated/legacy/pws/www/quran/search\u func\u array\u ret.PHP

但是,现在
search\u func\u array\u 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
语句将被解析的路径

如果这是问题所在,您需要修改
包含
/
要求
(提示:您可以使用
\uuuuuuu DIR\uuuuu
引用当前解析文件所在的目录,例如
要求(\uuuuuu DIR.'/core/db.inc.php');
)或将文件移动到正确的位置

3) 我不确定
bind\u result
是否会像您现在这样跨函数边界工作。如果发现
$sura
$aya
$text
为空,则需要移动
$stmt->bind_result($id、$sura、$aya、$text)出函数,在
while($stmt->fetch())
语句上方

然而,更好的方法是使用
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 />';
}
EDIT2:实际上,既然
fetch\u all
的优势已经消失,您也可以返回
$stmt->get\u result()
并将循环更改为:

$sqlResult = search_aya($query);
foreach($row = $sqlResult->fetch_assoc()) {
    echo $row['sura'] . '-' . $row['aya'];
    echo $row['text'];
    echo '<hr />';
}
$sqlResult=search\u-aya($query);
foreach($row=$sqlResult->fetch\u assoc()){
echo$row['sura'].-.$row['aya'];
echo$row['text'];
回声“
”; }
然后可以删除函数中对
bind_param
的调用

EDIT3:我刚刚意识到我之前犯了一个错误,我忘记了必须首先对语句调用
get\u result
才能接收results对象,然后才能调用
fetch\u all
fetch\u assoc
。我相应地更正了答案


请注意,
get_result
,以及之后的
fetch_all
fetch_assoc
仅在PHP可以使用本机SQL驱动程序时才起作用。如果不起作用,您需要继续使用旧方法,但只需移动行
$stmt->bind\u result($id、$sura、$aya、$text)函数外部。

哪一行有此错误?您可以删除
错误报告(-1)以了解它是如何运行的。删除错误报告(-1)后,这与“500-内部服务器错误”相同;使用
ini\u集('display\u errors',1);ini设置(“显示启动错误”,1);错误报告(E_全部)检查PGPEST中的错误启动调试模式格式化此代码,看在上帝的份上!是的,我确保db.inc.php确实存在。如果函数中有fetech关联数组,则该脚本工作正常。但是当“return$stmt;”在函数中写入时,它不工作,这不是日志告诉我的。但是如果你的问题只发生在这种情况下,那么这正是我在第3点中所想的,所以你不能这样做,因为
bind\u result
将绑定到函数内部的局部变量,而你无法访问外部变量。正确的解决方案是使用
fetch_all
并按照我的建议返回数据。(编辑:您刚刚发布的代码工作版本的更新证实了我的假设。您不能绑定到函数内部的变量,然后在函数外部调用
fetch
return。)返回$stmt->fetch_all(MYSQLI_ASSOC);在函数内部添加时出现以下错误:错误:调用/storage/emulated/legacy/pws/www/quran/search_func_array_ret.php中未定义的方法mysqli_stmt::fetch_all(),第54行显示您的php版本早于5.3.0。我在我的答案中添加了针对旧PHP的解决方案(见最底部)。或者,您当然可以继续返回
$stmt
本身,但不使用
bind\u result
,您只需使用
while($row=$stmt->fetch)
$sqlResult = search_aya($query);
foreach($row = $sqlResult->fetch_assoc()) {
    echo $row['sura'] . '-' . $row['aya'];
    echo $row['text'];
    echo '<hr />';
}