简体   繁体   中英

PHP add column and entry to a array in while loop

I making a web socket book search app and I want to add a column with entries to the MySQL query received. I will also do a json_encode to entryData . The query is like this.

$query = mysqli_query($con, "SELECT Title, Author, ISBN, Publisher, Year
                             FROM books
                                 WHERE Title = '$title' OR Author= '$author'
                                 OR isbn='$isbn' OR Year='$year' ");

$entryData = array();
while($row=mysqli_fetch_assoc($query))
{
   $entryData[] = $row;
   $entryData->category = $category;
}

Something is going completely wrong in my while loop , however when I add the data from the MySQL query manually to the entryData array it works. So I must be doing something wrong in the query.

...all category column will have the same values or entries.

So your while loop should be like this:

while($row=mysqli_fetch_assoc($query)){
    $row['category'] = $category;
    $entryData[] = $row;
}

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