what's wrong with this code?

it gives this error

Warning: Invalid argument supplied for foreach()

$result = mysql_query( "SELECT * FROM houses WHERE username ='Charles'" );
$row = mysql_fetch_array($result);
$names= $row['people'];
echo $equipas;
foreach($names as $key) 
{

echo $key;
}  

    while ($row = mysql_fetch_array($result)) {
    $names[]= $row['people'];
    }

    ought to populate the $names array as required.

    Blu

      If you are looking to echo data straight out of the query, why not try this:

      $result = mysql_query( "SELECT * FROM houses WHERE username ='Charles'" );
      while($row = mysql_fetch_array($result)))
      {
          echo $row["people"];
      }
      

        I need something like this:

        $data = array(5,6,7,8);
        foreach($data as $key)
        {
        if ($key == 8)
        {
        echo "it's ok";
        } else
        {
        echo "is not ok ";
        break;
        }
        
        }  

        however the array is a mysql array but I can make it work with an musql array

        thanks

          This works for me, and seems to do what you want. I'm using a table I already have called staff and a column within that for given_name (varchar 50)

          <?PHP
          
          include "common.php";
          $link = @mysql_connect($dbHost, $dbUser, $dbPass);
          $dbselect = @mysql_select_db($dbName);
          
          $query = "Select given_name from staff";
          $result = mysql_query($query);
          
          while ($row = mysql_fetch_array($result)) {
          $data[] = $row['given_name'];
          echo $row['given_name'];
          echo "<br>";
          
          }
          
          echo "<br>Now we sort through the array<br>";
          
          foreach($data as $key => $value) 
          // I'm looping through both Keys and Values
          { 
          if ($key == 4) 
          { 
          echo "it's ok ".$value."<br><br>"; 
          } else 
          { 
          echo "is not ok ".$value."<br><br> "; 
          } 
          
          } 
          
          ?>
          

          HTH - Blu

            I need something like this:

            The thing is, "something like this" is very vague.

            however the array is a mysql array but I can make it work with an musql array

            What's a "mysql array" and "musql array"?

            This is what I suspect you want to do:
            You have a few people values of the "people" column in your houses table. For the username 'Charles', you want to select with these values of the "people" column. As it stands, your idea is to select all the rows that have the username 'Charles', then use PHP to filter out based on the "people" column.

            In truth, you can do it all in the SQL statement by getting PHP to generate the SQL statement.

            $people_array = array("Peter", "John", "Mary");
            $people = "'" . implode("','", $people_array) . "'";
            $query = "SELECT * FROM houses WHERE username='Charles' AND people IN($people)";
            $result = mysql_query($query);
              Write a Reply...