简体   繁体   中英

select statement from SQL into PHP array

The following PHP code, I tried to retrieve some values from the table by "word" column which happy, like, free and help are in this column.

while($row=mysqli_fetch_array($result_kwp,MYSQLI_ASSOC))
        {
            $v_kw = $row["word"]." ";
            echo $v_kw;
        }
$category_text = array(
'positive' => $v_kw,
);
 echo "<b>positive</b> Category: ". $category_text['positive']."<br/>";

my output:

happy like free help 
positive Category: help 

my question is why $category_text['positive'] is contained only help word? How can I put every word in this array?

In your code, every iteration reassigns $v_kw to the new $row["word"] . You have to concatenate all your results by using .=

$v_kw = "";
while($row=mysqli_fetch_array($result_kwp,MYSQLI_ASSOC))
        {
            $v_kw .= $row["word"]." ";
            echo $row["word"];
        }
$category_text = array(
'positive' => $v_kw,
);
 echo "<b>positive</b> Category: ". $category_text['positive']."<br/>";

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