Jump to content

[SOLVED] Warning: extract() [function.extract]: First argument should be an array


Recommended Posts

Hello,

 

I'm slowly making my way thru a php/mysql book and have encountered an issue with one of their example programs.  I've followed the directions by the number and my php script connects fine to the DB i created, however i get the error:

Warning: extract() [function.extract]: First argument should be an array

 

I will paste my php script below (minus my connection include) and also add screenshots of 1- the specific output error and 2- the three tables in my DB.

 

Any help / thoughts / expertise (geared towards a novice in terms of language and explanation) would be appreciated.

 

CODE:

<?php

$link = REMOVED FOR OBVIOUS REASONS;

 

$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) or die(mysql_error());

    $row_d = mysql_fetch_array($results_d);

    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) or die(mysql_error());

    $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's name from people table

    get_director($movie_director);

 

    //get lead actor's name from people table

    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>Total :$num_movies Movies</td>

</tr>

EOD;

 

$movie_footer ="</table>";

 

$movie =<<<MOVIE

              $movie_header

              $movie_details

              $movie_footer

MOVIE;

 

    print "There are $num_movies movies in our database";

    print $movie;

?>

 

 

********************

ERROR:

 

error1wa3.jpg

 

********************

TABLES:

 

tablemovietypeerror1wt1.jpg

 

tablemovieerror1ip5.jpg

 

tablepeopleerror1or0.jpg

 

Thanks for any input.

How old is this book?  I see you're using globals as well which is bad practice.

 

I have no experience with the extract() function, but it looks to me as if you're trying to use it to retrieve a mysql result column value.(?)  You can also access it by

$row_d = mysql_fetch_array($results_d);
     $director = $row_d['people_fullname'];

 

If that is not what you're trying to do, tell me and I will try to assist differently.

 

There is also an easier method using list() to map values

If a query returns no data, mysql_fetch_array() will not return an array, causing the extract() to fail with that message.  So you should check if $row_a and $row_d are arrays first.  Bruce Almighty looks like it might be the problem, as the lead actor and director do not exist.

 

How old is this book?  I see you're using globals as well which is bad practice.

 

Thanks for the reply X.  The book covers PHP5, so it can't be too old.  The "global" refrence they use in this example has to do with the variable scope of the function:  http://us3.php.net/global.  I believe you might be thinking of the unsafe practice of having Using Register Globals set to ON in the php.ini file?

 

From my novice understanding, scope of function variables is an acceptable use?

 

 

 

I have no experience with the extract() function, but it looks to me as if you're trying to use it to retrieve a mysql result column value.(?)  You can also access it by

$row_d = mysql_fetch_array($results_d);
     $director = $row_d['people_fullname'];

 

If that is not what you're trying to do, tell me and I will try to assist differently.

 

I'm not sure what i'm trying to do to be honest. Just get their script to work so i understand it and it works perfectly fine and I can move to the next chapter haha

 

There is also an easier method using list() to map values

If a query returns no data, mysql_fetch_array() will not return an array, causing the extract() to fail with that message.  So you should check if $row_a and $row_d are arrays first.  Bruce Almighty looks like it might be the problem, as the lead actor and director do not exist.

 

B, you hit the nail on the head. I deleted that second "Bruce Almighty" row and the error disappeared.  I'll need to look into mysql_fetch_array()  to fully understand what you pointed out.  Thanks for finding the isssue.

 

J

for starters

 

use

 

mysql_fetch_assoc

 

secondly

 

it returns something similar to:

 

Array (
  [ NAME ] => Whatever
  [ AUTHOR ] => Whatever, JR (lol)
  [ DATE ] => Whenever
)

 

mysql_fetch_array gets

Array (
  [ 0 ] => Name Whatever
  [ NAME ] => Name Whatever
)

 

however you can use an optional arguement to mysql_fetch_array

 

mysql_fetch_array($resultResource,MYSQL_NUM);

for just 0 1 2 3

mysql_fetch_array($resultResource,MYSQL_ASSOC);

for just NAME AUTHOR DATE

 

but for the second one its less typing to just do mysql_fetch_assoc

 

and with assoc you can still foreach through the array so it makes not much difference lol

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.