user2720970
user2720970

Reputation: 294

MySQL Row in one output

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

Upvotes: 0

Views: 120

Answers (4)

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

Upvotes: 3

Madcoe
Madcoe

Reputation: 213

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.

Upvotes: 0

Shivam
Shivam

Reputation: 720

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.

Upvotes: 0

vee
vee

Reputation: 38645

Use implode to join the array elements:

echo implode(' ', $return_arr);

Upvotes: 3

Related Questions