将mysql数据库中的多行存储到一个变量中 [英] Store multiple rows from mysql database into one single variable

查看:472
本文介绍了将mysql数据库中的多行存储到一个变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这对我来说真的很难,我正在尝试将mysql中表中的多行存储到单个变量中

this is really hard for me, i'm trying to store multiple rows from my table in mysql into a single variable

$mysql_statementJc = "SELECT * FROM `dog`.`cat` WHERE `name` = '$name'";
$mysql_commandJc = mysql_query($mysql_statementJc);


 while($row = mysql_fetch_array($mysql_commandJc)){

    $followI = $row['industry'];

    $groupConcat = $followI . " OR ";

    echo $groupConcat;
}


这是我的方法,所以最后我可以将"sam,john,bob"保存为$ groupConcat,然后可以在其他地方使用.


This is my appproach, so at the end of it all i can get "sam, john, bob" saved as $groupConcat which can then be used elsewhere.

非常感谢.

推荐答案

每次循环,都将所需的值添加到数组中.然后,在循环之后,使用 implode()函数创建所需的输出

Each time through the loop, add the desired value to an array. Then, after the loop, use the implode() function to create your desired output.

while($row = mysql_fetch_array($mysql_commandJc)){

    $followI = $row['industry'];

    $resultArray[] = $followI;
}

$groupConcat = implode(" OR ", $resultArray);

echo $groupConcat;

请注意,您应该真正停止使用mysql库,而应使用 mysqli PDO .

Note that you should really stop using the mysql library and instead use mysqli or PDO.

这篇关于将mysql数据库中的多行存储到一个变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆