Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 23rd, 2012, 04:51 PM
Registered User
 
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
 
Old October 24th, 2012, 04:24 AM
Friend of Wrox
 
Join Date: May 2011
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;
}
 
Old October 24th, 2012, 09:57 AM
Registered User
 
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";

?>
 
Old October 24th, 2012, 10:47 AM
Friend of Wrox
 
Join Date: May 2011
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.
 
Old October 24th, 2012, 12:06 PM
Registered User
 
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





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 05:33 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.