Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to insert array into a MYSQL database?
    text
    copied!<p>This is my array for top 10 movies</p> <pre><code>$data() = Array ( [0] =&gt; Array ( [rank] =&gt; 1. [rating] =&gt; 9.2 [title] =&gt; The Shawshank Redemption [year] =&gt; 1994 [number_of_votes] =&gt; 643,339 ) [1] =&gt; Array ( [rank] =&gt; 2. [rating] =&gt; 9.2 [title] =&gt; The Godfather [year] =&gt; 1972 [number_of_votes] =&gt; 488,694 ) [2] =&gt; Array ( [rank] =&gt; 3. [rating] =&gt; 9.0 [title] =&gt; The Godfather: Part II [year] =&gt; 1974 [number_of_votes] =&gt; 301,665 ) [3] =&gt; Array ( [rank] =&gt; 4. [rating] =&gt; 8.9 [title] =&gt; The Good, the Bad and the Ugly [year] =&gt; 1966 [number_of_votes] =&gt; 202,341 ) [4] =&gt; Array ( [rank] =&gt; 5. [rating] =&gt; 8.9 [title] =&gt; Pulp Fiction [year] =&gt; 1994 [number_of_votes] =&gt; 507,363 ) [5] =&gt; Array ( [rank] =&gt; 6. [rating] =&gt; 8.9 [title] =&gt; 12 Angry Men [year] =&gt; 1957 [number_of_votes] =&gt; 154,104 ) [6] =&gt; Array ( [rank] =&gt; 7. [rating] =&gt; 8.9 [title] =&gt; Schindler's List [year] =&gt; 1993 [number_of_votes] =&gt; 337,187 ) [7] =&gt; Array ( [rank] =&gt; 8. [rating] =&gt; 8.8 [title] =&gt; One Flew Over the Cuckoo's Nest [year] =&gt; 1975 [number_of_votes] =&gt; 265,303 ) [8] =&gt; Array ( [rank] =&gt; 9. [rating] =&gt; 8.8 [title] =&gt; The Dark Knight [year] =&gt; 2008 [number_of_votes] =&gt; 578,207 ) [9] =&gt; Array ( [rank] =&gt; 10. [rating] =&gt; 8.8 [title] =&gt; Inception [year] =&gt; 2010 [number_of_votes] =&gt; 415,584 ) ) </code></pre> <p>Schema</p> <pre><code>mysql_query("CREATE TABLE TopMovies (rank INT, rating VARCHAR(3), title VARCHAR(50), year INT, number_of_votes INT )"); </code></pre> <p>This is my loop</p> <pre><code>foreach ($data as $movie =&gt; $rows) { foreach ($rows as $row =&gt; $columns) { $query = "INSERT INTO TopMovies (rank, rating, title, year, number_of_votes) VALUES ({$columns['rank']}, {$columns['rating']}, {$columns['title']}, {$columns['year']}, {$columns['number_of_votes']});"; } mysql_query($query); } </code></pre> <p>This is what I get, what's wrong?</p> <pre><code>+------+--------+-------+------+-----------------+ | rank | rating | title | year | number_of_votes | +------+--------+-------+------+-----------------+ | 6 | 6 | 6 | 6 | 6 | | 4 | 4 | 4 | 4 | 4 | | 3 | 3 | 3 | 3 | 3 | | 2 | 2 | 2 | 2 | 2 | | 5 | 5 | 5 | 5 | 5 | | 1 | 1 | 1 | 1 | 1 | | 3 | 3 | 3 | 3 | 3 | | 2 | 2 | 2 | 2 | 2 | | 5 | 5 | 5 | 5 | 5 | | 4 | 4 | 4 | 4 | 4 | +------+--------+-------+------+-----------------+ </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload