簡體   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