0

This code is reading from mysql db tables and should return few names. But each name is sent as separated string. I need it in one string. Here is code:

<?php
session_start();

function prList($REQUEST){

  include ('into_sql.php');
  mysql_select_db("db394771350", $conn);

  $usr = $REQUEST["usnm"];
  $fID = $REQUEST["friendID"];
  $sql = mysql_query("SELECT * FROM users WHERE username='$usr'");
  $a=mysql_fetch_array($sql);

  $uID = $a["id"];

  $res1 = mysql_query("SELECT * FROM friends WHERE friendID='$uID' AND accept='0'");
  while($b=mysql_fetch_array($res1)){
    $fuID = $b["userID"];

    $res2 = mysql_query("SELECT * FROM users WHERE id='$fuID'");

    while($c=mysql_fetch_array($res2)){

      $usrn = $c["username"];
      $array = array("$usrn");
      encode($array);
    }
  }
}
?>

This is output:

["rhys"]["alexroan"]["bobjosh"]["xc.j.gingex"]["tom"]

4 Answers 4

2
$usrn = "";
while($c=mysql_fetch_array($res2)){

$usrn .= $c["username"];
}
echo $usrn;
Sign up to request clarification or add additional context in comments.

Comments

0

How is the function encode defined? I would suggest replacing $array = array("$usrn"); by $array[] = $c["username"] and at the end just implode all entries into one string via `$string = implode($array)

4 Comments

this is what it returns: "ArrayArrayArrayArrayArray"
any idea how to put space between each record?
Yesh sure, simply use implode(' ', $array), nothing easier than that :) Why not upvoting my answer than? :)
would love to but my reputation is not above 15 so far so I can't. Sorry
0

Why don't you just concatenate results ?

$result = "";
$i = false;
while ($c=mysql_fetch_array($res2)){
   if ($i)
      $result .= ' ';
   $result .= $c["username"];
   $i = true;
}

echo $result;

8 Comments

not working returning this: ["rhys"]["rhysalexroan"]["rhysalexroanbobjosh"]["rhysalexroanbobjoshxc.j.gingex"]["rhysalexroanbobjoshxc.j.gingextom"]
The last string between brackets is the result you expect ? If it is, display the result at the exit of the loop not inside.
Thank you anyway solution from @Alexandrew solved my problem , but as I said thank you.
no problem. For your spaces problem try this: implode(' ', $array) instead of implode($array)
With a space between the quotes
|
0

Check this out may be helpfule forl

FIND_IN_SET(str,strlist)

Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by “,” characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL. This function does not work properly if the first argument contains a comma (“,”) character.

mysql> SELECT FIND_IN_SET('b','a,b,c,d'); -> 2

Comments

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.