简体   繁体   中英

PHP function will not return mysqli query results

I am having problems with this function. I believe it may be in the way that I wrote my query. Both "id" and "$number" are integers.

Could someone please tell me what I am doing wrong, or a better way to write it. Any help would be much apprecitiated.

<?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);

?>

A common, but simple mistake is to include a ->fetch_assoc(); call before/outside your while() loop -

$row = $result->fetch_assoc();

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

In your case, since you are only returning 1 row from your query, that result is returned in the 1st $row = $result->fetch_assoc(); , and then the internal pointer is moved forward.

So when you get to while($row = $result->fetch_assoc()){ there is no more results to return, as according to the docs -> Returns ... **NULL** if there are no more rows in resultset. .

If you were returning more than 1 row, you would find your issue would be that it would return all rows, except for the 1st.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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