phptexnoob Posted January 9, 2009 Share Posted January 9, 2009 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: ******************** TABLES: Thanks for any input. Quote Link to comment https://forums.phpfreaks.com/topic/140116-solved-warning-extract-functionextract-first-argument-should-be-an-array/ Share on other sites More sharing options...
xtopolis Posted January 9, 2009 Share Posted January 9, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/140116-solved-warning-extract-functionextract-first-argument-should-be-an-array/#findComment-733077 Share on other sites More sharing options...
btherl Posted January 9, 2009 Share Posted January 9, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/140116-solved-warning-extract-functionextract-first-argument-should-be-an-array/#findComment-733078 Share on other sites More sharing options...
phptexnoob Posted January 9, 2009 Author Share Posted January 9, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/140116-solved-warning-extract-functionextract-first-argument-should-be-an-array/#findComment-733087 Share on other sites More sharing options...
phptexnoob Posted January 9, 2009 Author Share Posted January 9, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/140116-solved-warning-extract-functionextract-first-argument-should-be-an-array/#findComment-733091 Share on other sites More sharing options...
RussellReal Posted January 9, 2009 Share Posted January 9, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/140116-solved-warning-extract-functionextract-first-argument-should-be-an-array/#findComment-733094 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.