如何将pdo返回的关联数组的内容放入数组中

I am using PHO PDO and getting it to return an associative array.

I can loop through the associative array created by PDO, but I cant work out how to return the array from a function.

function wantedrentals($provence, $country, $dbh)
   {
      echo "Now in wanted wanted rentals function<br><br>"; 

      $journeyrentalssarray = array();

      $sth = $dbh->query("SELECT summary, url, provence, country from tblAccomadation WHERE country = $country ");

    # setting the fetch mode
    $sth->setFetchMode(PDO::FETCH_ASSOC);

    while($row = $sth->fetch()) 
        {
            echo $row['summary'] . "
";
            echo $row['url'] . "
";
            echo $row['provence'] . "
";
            echo $row['country'] . "
";
        }

  return $journeyrentalssarray;
  }

I am very new to PHP and PDO and would appreciate any help.

function wantedrentals($provence, $country, $dbh)
   {
      echo "Now in wanted wanted rentals function<br><br>"; 

      $journeyrentalssarray = array();

      $sth = $dbh->query("SELECT summary, url, provence, country from     tblAccomadation WHERE country = '$country' ");

    # setting the fetch mode
    $sth->setFetchMode(PDO::FETCH_ASSOC);

    while($row = $sth->fetch()) 
        {
            $journeyrentalssarray[] = $row; // add $row to $journeyrentalssarray array
            echo $row['summary'] . "
";
            echo $row['url'] . "
";
            echo $row['provence'] . "
";
            echo $row['country'] . "
";
        }

  return $journeyrentalssarray;
  }

You may want it this way

function wantedrentals($provence, $country, $dbh)
   {
      echo "Now in wanted wanted rentals function<br><br>"; 

      $journeyrentalssarray = array();

      $sth = $dbh->query("SELECT summary, url, provence, country from tblAccomadation WHERE country = $country ");

    # setting the fetch mode
    //$sth->setFetchMode(PDO::FETCH_ASSOC);

    return $sth->fetch(PDO::FETCH_ASSOC);
  }

PDO::FETCH_ASSOC will returns an array indexed by column name as returned in your result set.

The return value of fetch function on success depends on the fetch type in your case its FETCH_ASSOC and FALSE is returned on failure.

For more information please read here

We can also use fetchAll to get an array of all of the result set rows. So the function may look like this.

function wantedrentals($provence, $country, $dbh)
       {
          echo "Now in wanted wanted rentals function<br><br>"; 

          $journeyrentalssarray = array();

          $sth = $dbh->query("SELECT summary, url, provence, country from tblAccomadation WHERE country = $country ");


        return $sth-> fetchAll(PDO::FETCH_ASSOC);
      }

For more information please read here