• PHP Help PHP Newbies
  • Warning: extract() expects parameter 1 to be array, boolean given in /customers/0/9/a

This error message appeared when I started working with the lessons in Beginning PHP... again, after I had abandoned the project about a year ago. I don't think that the error message was there last year. When I searched the web, several sites returned the same error message. Below is my coding, copied with care from the book. the full error message is "Warning: extract() expects parameter 1 to be array, boolean given in /customers/0/9/a/erinaceus.eu/httpd.www/php/filmsite/table2.php on line 37 Warning: extract() expects parameter 1 to be array, boolean given in /customers/0/9/a/erinaceus.eu/httpd.www/php/filmsite/table2.php on line 52", so I marked line 37 in the code below:

<?php
$link = mysql_connect("localhost","erinaceus_eu","*********") or die(mysql_error());
mysql_select_db("erinaceus_eu") or die (mysql_error());

 $query = "SELECT
                movie_name,
                movie_director,
                movie_leadactor
          FROM
                movie";

 $result = mysql_query($query,$link) or die(mysql_error());
 $num_movies = mysql_num_rows($result);

 $movie_header=<<<EOD
 <h2><center>Movie Review Database</center></h2>
 <table  width='70%' border='1' cellpadding='2'
   cellspacing='2' align='center'>
      <tr>
           <th>Movie Title</th>
           <th>Movie Director</th>
           <th>Movie Lead Actor</th>
      </tr>

EOD;

function get_director()
{
global $movie_director;
global $director;

$query_d="SELECT people_fullname
FROM people
WHERE people_id='$movie_director'";
$results_d=mysql_query($query_d);
$row_d=mysql_fetch_assoc($results_d);
/THIS IS THE OFFENDING LINE 37/extract ($row_d);
$director=$people_fullname;
}

function get_leadactor()
{
global $movie_leadactor;
global $leadactor;

$query_a="SELECT people_fullname
FROM people
WHERE people_id='$movie_leadactor'";
$results_a=mysql_query($query_a);
$row_a=mysql_fetch_array($results_a);
extract ($row_a);
$leadactor=$people_fullname;
}

while($row = mysql_fetch_array($result))
{
$movie_name = $row['movie_name'];
$movie_director = $row['movie_director'];
$movie_leadactor = $row['movie_leadactor'];
get_director($movie_director);
get_leadactor($movie_leadactor);

 $movie_details .=<<<EOD
 <tr>
      <td>$movie_name</td>
      <td>$director</td>
      <td>$leadactor</td>

 </tr>

EOD;
}

$movie_details .=<<<EOD
<tr>
<td> </td>
</tr>
<tr>
<td>Totalt: $num_movies filmer</td>
</tr>
EOD;

$movie_footer ="</table>";

$movie =<<<MOVIE
$movie_header
$movie_details
$movie_footer
MOVIE;
print "I databasen finns $num_movies filmer.";
print $movie;
?>

sincerly
Lennart H

    I posted a question about this warning message about an hour ago. The error message appears when I load my table file, and I have seen it several times when I used the string to search the web, on other pages. Was my question removed because I posted too much, or because I had the word W***ing in the title? I would very much appreciate a response, or at least seeing my q on the forum. here is the offending snippet again:

    function get_director()
    {
    global $movie_director;
    global $director;

    $query_d="SELECT people_fullname
    FROM people
    WHERE people_id='$movie_director'";
    $results_d=mysql_query($query_d);
    $row_d=mysql_fetch_assoc($results_d);
    extract ($row_d);
    $director=$people_fullname;
    }

    sincerely
    L. Hildeman

      Few problems I see with the get_director() function:

      1. Don't use global variables. Google "why global variables are bad" and pick some of the 2.4+ million hits to learn more.

      2. Has the data you're placing into that query been properly sanitized first? If not, you're just begging to become the victim of a SQL injection attack (and/or just plain SQL errors). See [man]security.database.sql-injection[/man] for an introduction to this topic if you've never heard of it.

      3. The entire [man]mysql[/man] extension has been outdated and superseded by the [man]MySQLi[/man] extension (or perhaps even [man]PDO[/man]). See [man]mysqlinfo.api.choosing[/man] for more info.

      4. You never check to see if [man]mysql_query/man returned boolean FALSE (indicating an error has occurred) before you attempt to retrieve a row from the result set.

      5. You never check to see if the result set contained at least one row (otherwise [man]mysql_fetch_assoc/man will simply return boolean FALSE).

      6. Don't use extract(); it makes your code harder to read/understand.

        lenhi wrote:

        I posted a question about this warning message about an hour ago. The error message appears when I load my table file, and I have seen it several times when I used the string to search the web, on other pages. Was my question removed because I posted too much, or because I had the word W***ing in the title? I would very much appreciate a response, or at least seeing my q on the forum.

        It simply was not approved from the moderation queue until now, so I merged the two threads and will be moving this thread to the Newbies forum shortly.

          lenhi;11008549 wrote:

          copied with care from the book.

          If the book you have really uses the mysql extension, uses global variables, doesn't sanitize data, uses "or die(mysql_error())", etc. then I think the first thing you need to do is to throw that book in the trash and find a new one. 😉

            Well, this is heady stuff, but it is probaly better to try and understand and learn from your remarks than from an eight year old book. Still, I would appreciate if you could explain a few things here:
            "Sanitize" is a new expression to me. I am trying to get my mind round the explanation in the link you provided. I don't quite understand your #5. Is there some sort of safety check I should include? PHP is difficult for me, and now I am more confused. HTML and CSS are so much easier, but I really need to figure this. The first hit about globals turned out to be a discussion, so yes, there is a lot of argument against globals, but how can I change my code and adapt it to the present-day situation? Do you have another book to suggest?

              When posting, please use the code markup tags described in the FAQs to mark up your code and make it readable.

              lenhi wrote:

              I don't quite understand your #5. Is there some sort of safety check I should include?

              Yes, you should be checking to see that [man]mysql_query[/man] ran without error (see the examples on that page), and handling both possible outcomes (i.e., there either was an error or there wasn't). Then you have to find out why there was an error (Did the connection fail? Or - more likely - did you pass it a broken query?)

              PHP is difficult for me ... HTML and CSS are so much easier, but I really need to figure this.

              That's only to be expected: PHP is a programming language, HTML and CSS are not. To learn how to use PHP you have to learn how to program.

              The first hit about globals turned out to be a discussion, so yes, there is a lot of argument against globals, but how can I change my code and adapt it to the present-day situation?

              The usual way to provide information to a function is to use so-called function arguments (the examples on that page are all of more-than-trivial cases; basically, it's arguments go in, result comes out).

              Do you have another book to suggest?

              I don't know about learning programming in general, since that depends on where you're starting from; but for PHP programming in particular a reasonable place to start is the Getting Started section in the PHP manual. I suppose doing a search for "learning programming". The first hit I got on that phrase was to http://norvig.com/21-days.html

                Ok, morning again here; I will mull over the [man]mysql_query[/man] command and function arguments and try to tune my brain to this language. HTML and CSS and various image editors have given me the moments of enlightenment and clarity that I assosciate with learning, but this has never occured in php och javascript. It's like playing music; interesting things happen, but relly training for a specific performance does not trigger enough reward hormones for me to stay focused, like when I study Latin or maths. So where I am is at a very basic, rather confused level, where I am trying to acquire enough competence and understanding to work with databases using the programming languages available. Thanx for taking the time to feed my mind!

                sincerely
                L. Hildeman

                  Write a Reply...