Jump to content


Photo

Problem using the explode function


  • Please log in to reply
2 replies to this topic

#1 freakness883

freakness883
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 13 July 2006 - 11:17 PM

I have the following code:

<?php
    $query3 = "SELECT members FROM groups WHERE g_id='".$id."'";
echo "$query3";
    $result3 = mysql_query("SELECT members FROM groups WHERE g_id='1'");
$row = mysql_fetch_row($result3);
    $pieces = explode(" ", $row);
echo $pieces[0];
echo $pieces[1];
if (in_array("bbaker", $pieces)) {
  echo "Got bbaker";
}
else{
  echo "nope";
}


echo "$query3" is printing out the proper select statement -- runnning the statement manually in MySQL gets me the record "bbaker bbarker". But when I echo $pieces[0] and $pieces[1] I get the output Array for both. I was hoping for bbaker and bbarker. And also I'm returning false on the condition in_array("bbaker", $pieces). Something's wrong with my SQL query. Any help is appreciated.

#2 Kurt

Kurt
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts
  • LocationNew York

Posted 13 July 2006 - 11:23 PM

It's because $row itself is an array, and you can't explode an array, only a string.

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 14 July 2006 - 12:29 AM

You want to use implode on the $row
<?php echo implode(' ',$row); ?>
Or you can just use echo on the $row entries:
<?php echo $row[0] . ' ' . $row[1]; ?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users