Jump to content

adding characters to select query


sparedog

Recommended Posts

Hi all,

I am new to php and new to this board. I have tried searching but cannot find anything for this.

 

If I echo the following code, it places all the results in a line, without spaces or line breaks. How would I separate the data by adding spaces or any other character for that matter.

 

Thanks

 

$sql = mysql_query("SELECT friendwith FROM friends WHERE member = '$myuname'");

while ($row = mysql_fetch_array($sql)) {

$result= "$row[friendwith]";

}

 

Link to comment
https://forums.phpfreaks.com/topic/177093-adding-characters-to-select-query/
Share on other sites

Hi sparedog,

 

Amend your code to read:

 

$sql = mysql_query("SELECT friendwith FROM friends WHERE member = '$myuname'");
while ($row = mysql_fetch_array($sql)) {
$result .= ''.$row[friendwith].'<br />';
}

 

The above code is adding a line break to the end of each $row[friendwith] which is getting output.

 

Hope this helps.

No sorry that didn't seem to do it.

 

I'll give you my whole code, maybe i'm doing something wrong elsewhere. What I'm trying to do is remove any duplicates from the final result $myfinal and I thought by separating, I could do this. Maybe there is another way to remove the duplicates from the $myfinal?

 

$myfriend_list = mysql_query("SELECT example FROM table1 WHERE member = '$myuname'");

while ($row1 = mysql_fetch_array($myfriend_list)) {

$result1= "$row1[example]";

 

$myoutput      = mysql_query("SELECT example2 FROM table2 WHERE username ='$result1'");

while ($row2 = mysql_fetch_array($myoutput)) {

$result2= "$row2[example2]";

 

$myfinal        = mysql_query("SELECT * FROM table3 WHERE example3 ='$result2'");

 

echo $myfinal;

}}

Hi

 

Not quite sure what you are trying to do as you appear to just be echoing out the result from your 3rd query.

 

What you could do is add each result to an array, and then once all are brought back you could use array_unique to get rid of the duplicates before outputting them.

 

However it would appear a better idea to use a single piece of SQL and use distinct (but whether this is useable depends on how complex you real SQL is rather than your slimmed down example). Something like:-

 

SELECT distinct c.* 
FROM table1 a
INNER JOIN table2 b
ON a.example = b.username
INNER JOIN table3 c
ON b.example2 = b.example3
WHERE a.member = '$myuname'

 

All the best

 

Keith

yes sorry.

 

i threw that echo on there without thinking about it. my real script actually goes on to say what's below. I was trying to not complicate things by throwing the echo on the end of the script I posted, but in doing so, I did complicate things ::)

 

The script functions, but it prints duplicates. Please ignore the script below it is only to show that the echo was added for the purpose of the post.

 

// Define $color=1

$color="1";

 

echo '<table width="300" border="0" align="center" cellpadding="0" cellspacing="0">';

while ($row3 = mysql_fetch_array($myfinal)) {

 

// If $color==1 table row color = #FFC600

if($color==1){

echo "<tr bgcolor='#F0F0F0'>

<td>".$row3['example4']."</td><td>".$row3['example5']."</td><td>".$row3['example6']."</td>

 

</td>

</tr>";

 

// Set $color==2, for switching to other color

$color="2";

}

 

// When $color not equal 1, use this table row color

else {

echo "<tr bgcolor='#D8D8D8'>

<td>".$row3['example4']."</td><td>".$row3['example5']."</td><td>".$row3['example6']."</td>

</tr>";

// Set $color back to 1

$color="1";

}

 

}

}

}

 

echo '</table>';

mysql_close();

?></body></html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.