Store multiple rows from mysql database into one single variable

后端 未结 3 1893
盖世英雄少女心
盖世英雄少女心 2021-01-24 12:23

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`.`ca         


        
相关标签:
3条回答
  • 2021-01-24 12:52

    You can also do it as:

    $followl = array();
    
    while($row = mysql_fetch_array($mysql_commandJc))
    { 
        $followl[] = $row['industry'];
    }
    
    $groupConcat = implode(" OR ",$followl);
    echo $groupConcat;
    

    Some Explanation:

    Store values into an array $followl and than use implode() function for imploding with OR.

    Side note:

    Please use mysqli_* or PDO becuase mysql_* is deprecated and no more available in PHP 7.

    0 讨论(0)
  • 2021-01-24 13:04

    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;
    

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

    0 讨论(0)
  • 2021-01-24 13:16

    you want to create a concatenated string, php has the .= syntax for this:

    $groupConcat='';
    
     while($row = mysql_fetch_array($mysql_commandJc)){
        $groupConcat .= $row['industry'] . " OR ";
    }
        echo $groupConcat; //no point echoing inside the loop
    

    sometimes a better approach is to use an array and implode()

    $groupConcat=array();
    
     while($row = mysql_fetch_array($mysql_commandJc)){
        $groupConcat[] = $row['industry'];
    }
        echo implode(' OR ',$groupConcat);
    
    0 讨论(0)
提交回复
热议问题