在PHP 5.2中是否有与mysqli_fetch_all相同的功能?

时间:2014-08-17 20:49:12

标签: mysqli undefined fetch fatal-error php-5.2

所以,我在PHP(和一般的后端编程)总共n00b,但我从来没有决定建立一个功能强大的数据库,用户可以搜索客户端作为我的第一个网络的最终项目开发课。

无论如何,我在本地主机上创建了我的站点和数据库,虽然花了一段时间,但我完成了所有功能。然而,当我试图将它移动到我的webhost时,一切都开始破坏了,因为我以前不知道,webhost使用的是PHP 5.2。我已经能够通过一些可疑的安全解决方案来安装其他一切(我知道,我知道,但我绝望了,而且这里只有假数据),但是我无法找到修复的东西是mysqli_fetch_all()。我调用了未定义的函数mysqli_fetch_all()错误。

我将它用于我的搜索功能:当您搜索用户时,该函数会获取所有具有匹配信息的行并将其放入数组中,最后,所有结果数组都合并到返回的单个数组。我是这样做的,因此未输入的搜索条件将被忽略而不会返回错误(NULL已成为我整个项目存在的祸根)。

可以在http://webinfofinal.webatu.com/profiles.html查看该网站,以便您了解我正在使用的内容,代码如下。有什么建议?我尝试了其他获取功能,但它们只返回第一个匹配的行。

        if ($firstName != null){
            $result2 = mysqli_query($con, "SELECT  displayName , firstName , lastName , eMail , age , classification , major , faveAnimes  FROM a2097702_fac.members where firstName =  '$firstName' ");
            $query2 = mysqli_fetch_all($result2,MYSQLI_ASSOC);
            $search = array_merge_recursive($search, $query2);
        }
        if ($lastName != null){
            $result3 = mysqli_query($con, "SELECT  displayName , firstName , lastName , eMail , age , classification , major , faveAnimes  FROM a2097702_fac.members where lastName =  '$lastName' ");
            $query3 = mysqli_fetch_all($result3,MYSQLI_ASSOC);
            $search = array_merge_recursive($search, $query3);
        }
        if ($eMail != null){
            $result4 = mysqli_query($con, "SELECT  displayName , firstName , lastName , eMail , age , classification , major , faveAnimes  FROM a2097702_fac.members where eMail =  '$eMail' ");
            $query4 = mysqli_fetch_all($result4,MYSQLI_ASSOC);
            $search = array_merge_recursive($search, $query4);
        }
        if ($age != null){
            $result5 = mysqli_query($con, "SELECT  displayName , firstName , lastName , eMail , age , classification , major , faveAnimes  FROM a2097702_fac.members where age =  '$age' ");
            $query5 = mysqli_fetch_all($result5,MYSQLI_ASSOC);
            $search = array_merge_recursive($search, $query5);
        }
        if ($classification != null){
            $result6 = mysqli_query($con, "SELECT  displayName , firstName , lastName , eMail , age , classification , major , faveAnimes  FROM a2097702_fac.members where classification =  '$classification' ");
            $query6 = mysqli_fetch_all($result6,MYSQLI_ASSOC);
            $search = array_merge_recursive($search, $query6);
        }
        if ($major != null){
            $result7 = mysqli_query($con, "SELECT  displayName , firstName , lastName , eMail , age , classification , major , faveAnimes  FROM a2097702_fac.members where major =  '$major' ");
            $query7 = mysqli_fetch_all($result7,MYSQLI_ASSOC);
            $search = array_merge_recursive($search, $query7);
        }           
        if ($faveAnimes != null){
            $result8 = mysqli_query($con, "SELECT  displayName , firstName , lastName , eMail , age , classification , major , faveAnimes  FROM a2097702_fac.members where faveAnimes =  '$faveAnimes' ");
            $query8 = mysqli_fetch_all($result8,MYSQLI_ASSOC);
            $search = array_merge_recursive($search, $query8);
        }

        if ($search != null){
            echo "<html>";
            echo "<head>";
            echo"<title> Anime Club Search Results | Web Info Final Project </title>";
            echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"webinfofinal.css\">";
            echo "</head>";
            echo "<div class=\"content\" style=\"width:50%; margin-left:-20%;\">";
                echo "<div class=\"header\">";
                    echo "<p></p><p>Your search results are below. </p>";
                echo "</div>";
                echo "<pre>";
                    print_r($search);
                echo "</pre>";
                echo "<p>End of results. <a href=\"profiles.html\">Search again?</a></p>";
                echo "<a href=\"login.html\"><input type='button' value='Update My Profile' id='updateProfile'></a>";
                echo "<a href=\"logout.php\"><input type='button' value='Log Out' id='logout'></a>";
            echo "</div>";
            echo "</html>";
        }

2 个答案:

答案 0 :(得分:1)

不,仅当您使用mysqlnd library时,fetch_all方法才可用。

可以为PHP 5.2编译mysqlnd(参见http://blog.ulf-wendel.de/2007/php-compiling-mysqlnd-with-php-525360/)但是如果您的Web托管服务提供商还没有准备升级到当前版本的PHP,我怀疑他们是否会安装mysqlnd对你而言。

您可以将代码转换为支持PDOStatement::fetchAll()的PDO。

或者您可以为mysqli编写自己的fetch_all函数,这并不难:

function fetch_all($result)
{
   $rows = array(); -- old array syntax still needed in PHP 5.2
   while ($row = $result->fetch_assoc())
          $rows[] = $row;

   return $rows;
}

我建议你找一个不同的网络主机。 PHP 5.2正式终止,可能存在安全漏洞,这些漏洞在更高版本的PHP中得到修复,但在5.2中没有。见http://php.net/eol.php

答案 1 :(得分:0)

感谢您的回答。我肯定会搜索新的主机,但由于我的时间很短,我做了一个代码解决方法。你的后一种解决方案或多或少是我最终做的。我查询了每一行,然后使用以下代码将具有匹配条件的行添加到数组中:

$result1 = mysql_query("SELECT  displayName , firstName , lastName , eMail , age , classification , major , faveAnimes  FROM a2097702_fac.members ".$parameterString);
            while ($row = mysql_fetch_array($result1, MYSQL_ASSOC)){
                array_push($results, $row);
            }
相关问题