问题
So, I am a total n00b at PHP (and back-end programming in general), but I never the less decided I wanted to build a functioning database that users could search client-side as th final project for my first web dev class.
Anyway, I made my site and database on a localhost, and though it took a while, I got everything functioning perfectly. When I tried to move it to my webhost, however, everything started breaking because, previously unbeknownst to me, the webhost is using PHP 5.2. I've been able to rig everything else with some functioning solutions of dubious security (I know, I know, but I'm desperate, and there's only fake data on here anyway), but the one thing I can't find a fix for is the mysqli_fetch_all(). I get a Call to undefined function mysqli_fetch_all() error.
I'm using it for my search functionality: when you search for a user, the function takes all the rows with matching information and puts it in an array, and at the end, all the result arrays are merged into a single array that is returned. I did it this way so that search criteria that is not entered would be ignored and not return an error (NULL has been the bane of my existence this entire project).
The site can be viewed at http://webinfofinal.webatu.com/profiles.html so you see what I'm working with and the code is below. Any suggestions? I've tried other fetch functions, but they only return the first matching row.
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>";
}
回答1:
No, the fetch_all
method is available only if you use the mysqlnd library.
It is possible to compile mysqlnd for PHP 5.2 (see http://blog.ulf-wendel.de/2007/php-compiling-mysqlnd-with-php-525360/) but if your web hosting provider isn't ready to upgrade to a current version of PHP, I doubt they'll install mysqlnd for you.
You can convert your code to PDO, which supports PDOStatement::fetchAll().
Or you can code your own fetch_all
function for mysqli, which is not hard:
function fetch_all($result)
{
$rows = array(); -- old array syntax still needed in PHP 5.2
while ($row = $result->fetch_assoc())
$rows[] = $row;
return $rows;
}
I recommend you find a different web host. PHP 5.2 is officially end-of-life, and there may be security vulnerabilities that are fixed in later version of PHP, but not in 5.2. See http://php.net/eol.php
回答2:
Thank you for your answer. I will definitely be searching fora new host, but since I was short on time, I made do with a code workaround. Your latter solution is more or less what I ended up doing. I queried each row then adding the ones with matching criteria to an array using this code:
$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);
}
来源:https://stackoverflow.com/questions/25353488/is-there-a-functional-equivalent-to-mysqli-fetch-all-in-php-5-2