-5

here is my code

$query = mysql_query("SELECT * FROM accommodation_vacancies WHERE accommodation_id = '$accom'");
$results = mysql_fetch_array($query);
if($query === FALSE) {
    die(mysql_error()); 
} else {
    print_r($results);
    foreach ($results as $result) {
        echo $result['start_date']; echo "<br/>";
    }           
}

And here is my output

enter image description here

By using the print_r commant i can see that The variable $results works properly,the query works properly also, i guess that i have mistakes on the foreach loop. Thank you.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
user3537552
  • 17
  • 1
  • 7
  • 3
    Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). – Jay Blanchard Dec 15 '14 at 14:14

1 Answers1

6

You are only fetching a single result. Use a while loop instead.

while ($result = mysql_fetch_array($query)) {

Side note: As stated in the comments, the mysql_* functions are deprecated. You should NOT learn how to use mysql using these deprecated methods. They will be removed from PHP in some future version and your code will stop working then. If you learn it, use mysqli_* or PDO.

Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
  • yes but for another occasion the results might be greater than one. – user3537552 Dec 15 '14 at 14:15
  • It doesn't matter how many results there are. The way you do it you only fetch one. The rest is discarded. The common way to fetch all results is a while loop. – Gerald Schneider Dec 15 '14 at 14:17
  • If this answer helped you out, please mark it as a valid answer on the left side directly under the vote-down-button :) – Jannik Dec 15 '14 at 14:36