繁体   English   中英

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

[英]PHP function will not return mysqli query results

我在使用此功能时遇到问题。 我相信这可能是我编写查询的方式。 “ id”和“ $ 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);

?>

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

$row = $result->fetch_assoc();

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

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

因此,当您进入while($row = $result->fetch_assoc()){ ,没有更多的结果要返回,如根据文档-> Returns ... **NULL** if there are no more rows in resultset.

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM