0

Im pretty new to MySQl and PHP. I need some help with a small issue.

My statement is as follows:

while($row = $stmt->fetch()) {
  $return_arr[] =  $row['name'];
      $return_arr[] =  $row['value'];
}

It outputs as follows:

Mr James Jones
23

How can I bring it together into one line? Like this:

Mr James Jones 23

Thank you

4
  • 5
    Try ... oh never mind, there's already an answer. Commented Jan 31, 2014 at 5:48
  • @Fred-ii- Troll happens :) Edited: You have more votes on that comment than you could get on posting an answer :p Commented Jan 31, 2014 at 6:02
  • True, as do other things, but I won't write it on here. There may be youngsters watching. ;-) @Rikesh Edited: (Tip: It doesn't smell very good) Commented Jan 31, 2014 at 6:06
  • LOL, not bad eh? Yet, I never put in answers when I don't see more code. As I stated in Shankar's answer, the explanation is all in there. Questions like these usually tend to open up, what I call a "can of worms". @Rikesh I was going to put in the very same thing as Shankar, till he put in an answer, that's why I wrote that, at the very instant I was writing "Try" Commented Jan 31, 2014 at 6:07

4 Answers 4

3

Use implode to join the array elements:

echo implode(' ', $return_arr);
Sign up to request clarification or add additional context in comments.

Comments

3

Do like this

while($row = $stmt->fetch()) {
  $return_arr[] =  $row['name']." ".$row['value'];
}

//print_r($return_arr); // The results gets printed as you expected or you could make use of a foreach construct as shown below.

//Printing using a foreach construct
foreach($return_arr as $k=>$v)
{
 echo $v;echo "<br>";
}

11 Comments

I was going to put that in a comment actually hahahaha +1 ;-)
lol fred. Answers should not be given under comments box that is what the comments tip says :P
True, but I'm always afraid that it will open up a "can of worm" ;-) and many downvote because of duplicate answers. It's happened to me before :(
Editted comment above -^
More often than me then lol! Ok, will hop on there tomorrow evening then. Will tweet you. Take care and get yourself one of those fancy coffee cups with an anti-spill rim ;-) You'll thank me for it. ciao - Edit: Btw, I was "right".
|
0

Just use like this,

while($row = $stmt->fetch()) {
  $return_arr[] =  $row['name']. ' '.$row['value'];
}

it will work. And if you want any other help let me know.

Comments

0

An alternate way is handling it inside your mysql query:

SELECT CONCAT(name, ' ', value) as name_and_value FROM ....

Than you can use

$row['name_and_value']

in your php code.

3 Comments

Thats great, working perfectly. Only question is how do I add and additional space or even text between the 2 outputs? Mr James Jones 23, to Mr James Jones "IS" 23
You can change blankspace between two fields to any character or string. Example SELECT CONCAT(name, 'add any string here', value) as name_and_value FROM ....
CONCAT means Concatenation. So php equvalent of this sql would be $name.' '.$value. You can also add any number of fields/strings inside concat like CONCAT(field1,' IS ',{$aphpvariable},' but ', field2, field3)

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.