Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0
This is the forum to discuss the Wrox book Beginning PHP5, Apache, and MySQL Web Development by Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K. Glass; ISBN: 9780764579660
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 23rd, 2012, 04:51 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default ch 3 Exercise 1

I am working through exercise 1 at the end of chapter 3 and am having a problem. The book code is not in the book zip file so I entered the code and am getting the following error

Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in C:\xampp\htdocs\ch3\ch3assign1.php on line 17

the header to the table displays properly but the mysql is not correct and I can't figure out where. Too be honest I'm not entirely sure how this works so it is difficult to find the error. Here is my code.

Code:
<?php

//connect to mysql
$connect = mysql_connect("localhost", "bp5am", "bp5ampass")
	or die("Check your server");
	
//connect to database
mysql_select_db("moviesite")
	or die(mysql_error());
	
//function to get lead actor
function get_leadactor($lead_actor) {
	global $actorname;
	$query2 = "SELECT people_fullname " .
			"FROM people " .
			"WHERE people.people_id = $lead_actor ";
	$results = mysql_fetch_array($query2)
		or die(mysql_error());
	extract ($rows);
	$actorname = $people_fullname;
	}
	
//function to get director
function get_director($director) {
	global $directorname;
	$query2 = "SELECT people_fullname " .
			"FROM people " .
			"WHERE people.people_id = $director ".
	$results = mysql_fetch_array($query2)
		or die(mysql_error());
	extract ($rows);
	$directorname = $people_fullname;
	}

//create the table
//format a header row
echo "<table border = '1'>\n";
	echo "<tr>\n";
		echo "<td><strong>Movie Name</strong></td>\n";
		echo "<td><strong>Lead Actor</strong></td>\n";
		echo "<td><strong>Director</strong></td>\n";
	echo "</tr>\n";
		
//get the movies
$query = "SELECT * FROM movie ";
$results = mysql_query($query)
	or die (mysql_error());
	
while ($rows = mysql_fetch_assoc($results)) {
	extract ($rows);
	
	//run functions
	get_leadactor($movie_leadactor);
	get_director($movie_director);
	
	//build the rest of the table
	echo "<tr>\n";
		echo "<td>\n";
		echo "$moviename";
		echo "</td>\n";
		echo "<td>\n";
		echo "$actorname";
		echo "</td>\n";
		echo "<td>\n";
		echo "$directorname";
		echo "</td>\n";
	echo "</tr>\n";
	}
	
echo "</table>\n";

?>

Last edited by Phrog; October 23rd, 2012 at 04:53 PM. Reason: added code tags
Reply With Quote
  #2 (permalink)  
Old October 24th, 2012, 04:24 AM
Friend of Wrox
Points: 651, Level: 9
Points: 651, Level: 9 Points: 651, Level: 9 Points: 651, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: North East UK
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

In the 2 functions you setup a query but do not execute it, so the function 'get_leadactor' should be like this;
Code:
//function to get lead actor
function get_leadactor($lead_actor)
{
	global $actorname;

	$query2 = "SELECT people_fullname " .
			"FROM people " .
			"WHERE people.people_id = $lead_actor ";
	$results = mysql_query($query2) or die (mysql_error());

	$rows = mysql_fetch_array($results) or die(mysql_error());

	extract ($rows);

	$actorname = $people_fullname;
}
Reply With Quote
  #3 (permalink)  
Old October 24th, 2012, 09:57 AM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, I did the change (thanks by the way) but I am still getting an error.

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\ch3\ch3assign1.php on line 33
Query was empty

the new code is
Code:
<?php

//connect to mysql
$connect = mysql_connect("localhost", "bp5am", "bp5ampass")
	or die("Check your server");
	
//connect to database
mysql_select_db("moviesite")
	or die(mysql_error());
	
//function to get lead actor
function get_leadactor($lead_actor) {
	global $actorname;
	$query2 = "SELECT people_fullname " .
			"FROM people " .
			"WHERE people.people_id = $lead_actor ";
	$results = mysql_query($query2)
		or die(mysql_error());
	$rows = mysql_fetch_array($results)
		or die(mysql_error());
	extract($rows);
	$actorname = $people_fullname;
	}
	
//function to get director
function get_director($director) {
	global $directorname;
	$query2 = "SELECT people_fullname " .
			"FROM people " .
			"WHERE people.people_id = $director ".
	$results = mysql_query($query2)
		or die(mysql_error());
	$rows = mysql_fetch_array($results)
		or die(mysql_error());
	extract($rows);
	$directorname = $people_fullname;
	}

//create the table
//format a header row
echo "<table border = '1'>\n";
	echo "<tr>\n";
		echo "<td><strong>Movie Name</strong></td>\n";
		echo "<td><strong>Lead Actor</strong></td>\n";
		echo "<td><strong>Director</strong></td>\n";
	echo "</tr>\n";
		
//get the movies
$query = "SELECT * FROM movie";
$results = mysql_query($query)
	or die (mysql_error());
	
while ($rows = mysql_fetch_assoc($results)) {
	extract ($rows);
	
	//run functions
	get_leadactor($movie_leadactor);
	get_director($movie_director);
	
	//build the rest of the table
	echo "<tr>\n";
		echo "<td>\n";
		echo "$movie_name";
		echo "</td>\n";
		echo "<td>\n";
		echo "$actorname";
		echo "</td>\n";
		echo "<td>\n";
		echo "$directorname";
		echo "</td>\n";
	echo "</tr>\n";
	}
	
echo "</table>\n";

?>
Reply With Quote
  #4 (permalink)  
Old October 24th, 2012, 10:47 AM
Friend of Wrox
Points: 651, Level: 9
Points: 651, Level: 9 Points: 651, Level: 9 Points: 651, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: North East UK
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

You have a . instead of a ; at the end of the query to grab the directors name.
Reply With Quote
  #5 (permalink)  
Old October 24th, 2012, 12:06 PM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That worked! Thanks so much, the devil is always in the details ...

Phrog
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Exercise 1 from Ch 11 akumok BOOK: Beginning ASP.NET 4 : in C# and VB 7 July 20th, 2012 02:17 AM
My solution to exercise 6, on ch.2 - help rjm BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 July 28th, 2010 05:50 PM
Ch 11 Pg459 Exercise 5 C# brivad BOOK: Beginning ASP.NET 1.0 0 September 10th, 2007 08:59 AM
Ch 28 exercise using Eclipse keeotee BOOK: Beginning JavaServer Pages 1 April 2nd, 2006 12:09 PM
Ch. 18 Exercise 1, is there a change? horseatingweeds BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 4 November 29th, 2005 04:33 PM



All times are GMT -4. The time now is 03:06 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.