 |
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
|
|
|
|

October 23rd, 2012, 04:51 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

October 24th, 2012, 04:24 AM
|
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
|
|
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;
}
|
|

October 24th, 2012, 09:57 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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";
?>
|
|

October 24th, 2012, 10:47 AM
|
|
Friend of Wrox
|
|
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
|
|
Greetings,
You have a . instead of a ; at the end of the query to grab the directors name.
|
|

October 24th, 2012, 12:06 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
That worked! Thanks so much, the devil is always in the details ...
Phrog
|
|
 |