Jam1 Jam1 - 7 months ago 42
SQL Question

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.

Answer

...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;
}