1

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

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.

Many thanks.

3
  • Do you mean you want to store them in an array variable? What have you tried already and what error are you encountering? Commented Feb 8, 2016 at 20:47
  • I have tried concatenating all of the variable in the while loop and that won't work, i don't know if arrays will work best as when i get the name e.g john i want to seperate it and put a OR in e.g "john OR sam OR dylan" which would be stored as a variable Commented Feb 8, 2016 at 20:49
  • What if you put a dot before the equal sign like this: $groupConcat .= $followI . " OR "; Also, move the echo after the while block. Commented Feb 8, 2016 at 20:50

3 Answers 3

3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

In ist solution OP will get the OR on end in last iteration . second solution is best one
trivial to remove the extra OR
2

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.

Comments

0

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.

1 Comment

$followl vs $followI

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.