PHP函数不会返回mysqli查询结果


PHP function will not return mysqli query results

我的这个函数有问题。我相信这可能是我写问题的方式。"id"answers"$number"都是整数。

有人能告诉我我做错了什么吗,或者用一种更好的方式写出来。任何帮助都会得到极大的感激。

<?php 
$number = htmlspecialchars($_GET['id']);

function getrecipeinfo($testing){
global $con;

$sqldescription = "SELECT category, eliquidname, image, contentnicpg, contentnicvg, description FROM vapetable where id = '{$number}' ;";
$result = mysqli_query($con, $sqldescription);
$row = $result->fetch_assoc();

     while($row = $result->fetch_assoc()){

        $eliquidtitle = $row['eliquidname'];
        $category = $row["description"];
        print $eliquidtitle;

             }
}
getrecipeinfo($testing);
?>

一个常见但简单的错误是在while()循环-之前/之外包含->fetch_assoc();调用

$row = $result->fetch_assoc();
while($row = $result->fetch_assoc()){

在您的情况下,由于您只从查询中返回1行,因此该结果将在第一个$row = $result->fetch_assoc();中返回,然后内部指针将向前移动。

因此,当您到达while($row = $result->fetch_assoc()){时,没有更多的结果可返回,如文档->Returns ... **NULL** if there are no more rows in resultset.所示。

如果您返回的行超过1行,您会发现问题是它将返回除第一行以外的所有行。

您应该传递$number(getrecipeinfo($number);),我有一个适用于PHP的Mysqli类,我共享我的存储库https://github.com/dev-lusaja/ezMysql