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 PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6
This is the forum to discuss the Wrox book Beginning PHP, Apache, MySQLWeb Development by Michael K. Glass, Yann Le Scouarnec, Elizabeth Naramore, Gary Mailer, Jeremy Stolz, Jason Gerner; ISBN: 9780764557446
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 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 Search this Thread Display Modes
  #1 (permalink)  
Old June 1st, 2004, 11:47 AM
Authorized User
 
Join Date: May 2004
Location: Charlestown, MA, USA.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Undefined Variable - Chapter 4

I have copied the code for "test2.php" from the downloaded code provided so I have a high confidence that it is correct and complete. Unfortunately, when I run "table2.php" I get the following error: "Notice: Undefined variable: movie_details in C:\Program Files\Apache Group\Apache2\test\table2.php on line 66". Line 66 is " $movie_details .=<<<EOD" the first mention of variable "$movie_details". Have I failed to set something in my Apache or PHP configuration? Is this perhaps caused by some upgrade in PHP since the book was written? Most of all, what is the fix to get rid of the error?

Reply With Quote
  #2 (permalink)  
Old June 1st, 2004, 03:08 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Ok, undefined variable notices are intended to help you as the programmer locate potential bugs and security problems in your scripts. If $movie_details is not initialized explicitly with a value before it is used then often times this can lead to logic errors or security holes.

That said, to alleviate the E_NOTICE level error, you need to either initialize the variable with a value as in:

$movie_details = (string) '';

Before it is concatenated using the .= combination of operators (concatenation and assignment) since this combination of operators expects $movie_details to already be created and already contain a value.

Or you can make use of language constructs like isset() and empty(), which being constructs that have a special purpose and not functions won't trigger the E_NOTICE level error when you test for a variable's existence or value with their use.

http://www.php.net/isset
http://www.php.net/empty

As far as why the book contains the error, I dunno. I don't even have the book.

Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
Reply With Quote
  #3 (permalink)  
Old June 1st, 2004, 06:20 PM
Authorized User
 
Join Date: May 2004
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The message you are getting is not really an error. It is a notice, and it is appearing because in your ini file Error_reporting is set to E_ALL. That is a good thing, so leave it that way. I like having it set like that so that I can catch all errors, warnings, and notices.

My suggestion is to add a line just before the while() statement that "$movie_details .=<<<EOD" is in. That line should read:

$movie_details = "";

As Richard has stated, that will initialize the variable, and you won't get a notice.

Reply With Quote
  #4 (permalink)  
Old June 2nd, 2004, 05:00 PM
Authorized User
 
Join Date: May 2004
Location: Charlestown, MA, USA.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the help. I have defined all the "Undefined variables" for which Notices have been sent to the browser. I have now proceeded to the last version of movie_details.php in Chapter 4. I now have a "Warning" and a group of similar "Notices". The Warning is: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Group\Apache2\test\movie_details.php on line 123 Line 123 of the code is: while($review_row = mysql_fetch_array($review_result)) The Notice that I am getting is:Notice: Uninitialized string offset: 0 in C:\Program Files\Apache Group\Apache2\test\movie_details.php on line 157

Notice: Uninitialized string offset: 0 in C:\Program Files\Apache Group\Apache2\test\movie_details.php on line 158

Notice: Uninitialized string offset: 0 in C:\Program Files\Apache Group\Apache2\test\movie_details.php on line 159

Notice: Uninitialized string offset: 0 in C:\Program Files\Apache Group\Apache2\test\movie_details.php on line 160

Notice: Uninitialized string offset: 0 in C:\Program Files\Apache Group\Apache2\test\movie_details.php on line 161
  Those lines follow: <td width='15%' valign='top' align='center'>$review_date[$i]</td>
          <td width='15%' valign='top'>$review_title[$i]</td>
          <td width='10%' valign='top'>$reviewer_name[$i]</td>
          <td width='50%' valign='top'>$review[$i]</td>
          <td width='10%' valign='top'align='center'>$review_rating[$i]</td>
My guess is that all these notices could be avoided by turning off the error reporting in the ini file. After all, I am not an experienced MySQL or PHP programmer (that's why I'm doing these excercises and reading the book) and I have no clue as to what to do about these "error" notices nor much of any appreciation for the good "practices" these messages are trying to convey. This might just save us all a bunch of time and agrivation.
Your helpful advice is appreciated.
JAH

Reply With Quote
  #5 (permalink)  
Old June 3rd, 2004, 01:06 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Hi John, since I was just talking about the same thing in another thread I'll point you to that thread.
http://p2p.wrox.com/topic.asp?TOPIC_ID=14255

Please let us know if you have any more questions.

Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
Reply With Quote
  #6 (permalink)  
Old June 3rd, 2004, 03:32 AM
Authorized User
 
Join Date: May 2004
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Richard, that thread won't help him.

The problem is that the $review_result variable doesn't contain any data. As a result, $review_row is not populated, and the notices are telling you that. If you fix the first problem, the notices will go away. This is a prime example why having error_reporting set to E_ALL is a good thing. It forces you to write proper code.

Please note that much of the code does not take error_reporting = E_ALL into account, in the interest of keeping the code as tight as possible. The book would be twice as long if we wrote it with error checking and trapping in mind.

John, first make sure that $review_query contains the correct SQL statement. You can do this by simply echoing it to the screen, then using quit(); If the query looks ok, then try going into MySQL and entering the query there. If an error is generated, then you're on your way to fixing it. If no rows are returned, make sure the table has data in it, and look over the query carefully to see if there is something wrong with it.

That should get you started. Let us know if you still have problems.

Good Luck!
Mike
Reply With Quote
  #7 (permalink)  
Old June 3rd, 2004, 12:46 PM
Authorized User
 
Join Date: May 2004
Location: Charlestown, MA, USA.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Greetings and thank you for the advice and help.
Unfortunately, I remain clueless as to what's going on. The code I am using has been copied from this site and matches exactly what's in the book. The only changes I have made (other than supplying the correct username/password are as recommended above. I had to add quite a few variable declarations to get rid of all the Undefined Variable notices. Here is what I have done (please note the lines I added I colored red for easy identification):
$review_table_headings=<<<EOD
     <tr>
          <th>Date of Review</th>
          <th>Review Title</th>
          <th>Reviewer Name</th>
          <th>Movie Review Comments</th>
          <th>Rating</th>
     </tr>
EOD;

$movie_takings = NULL;
$movie_cost = NULL;
$movie_name = NULL;
$movie_year = NULL;
$director = NULL;
$leadactor = NULL;
$movie_running_time = NULL;

while($row = mysql_fetch_array($movie_result))
{
     $movie_name = $row['movie_name'];
     $movie_director = $row['movie_director'];
     $movie_leadactor = $row['movie_leadactor'];
     $movie_year = $row['movie_year'];
     $movie_running_time = $row['movie_running_time']." mins";
     $movie_takings = $row['movie_takings'];
     $movie_cost = $row['movie_cost'];

     //get director's name from people table
     get_director($movie_director);

     //get lead actor's name from people table
     get_leadactor($movie_leadactor);

}

$review_result = "";
$review_flag = "";

while($review_row = mysql_fetch_array($review_result))
{
     $review_flag =1;
     $review_title[] = $review_row['review_name'];
     $reviewer_name[] = ucwords($review_row['review_reviewer_name']);
     $review[] = $review_row['review_comment'];
     $review_date[] = $review_row['review_date'];
     $review_rating[] = generate_ratings($review_row['review_rating']);
}

$review_query = "SELECT
                         *
               FROM
                         reviews
               WHERE
                          review_movie_id ='".$_GET['movie_id']."'
               ORDER BY
                          review_date DESC";

$review_result = mysql_query($review_query,$link) or die(mysql_error());

$i=0;

$review = "";
$review_date = "";
$review_title = "";
$reviewer_name = "";
$review_rating = "";
$review_details = "";

while($i<sizeof($review))
{
     $review_details .=<<<EOD
     <tr>

Next I went into MySQL in the command line monitor and (after finally figuring out how to sign in as "root" and how to "USE wiley" - I did mention I am unexperienced at SQL) I did successfully run the query and it returned the correct data with no errors. I further verified this because I am running phpMyAdmin. In there I see the data as it should be.
Perhaps I put one or more of the variable declarations in the wrong part of the code and it killed data that it might have been holding??!! Again, I am totally stuck. Any help is greatly appreciated.
Best,
JAH

Reply With Quote
  #8 (permalink)  
Old June 3rd, 2004, 01:02 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Hi John,
Sorry about my misunderstanding your question, as it happens, I just skimmed over your reply. Undefined offset and undefined string offset refer to array indices.

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Group\Apache2\test\movie_details.php

This error causes a domino effect to happen, as you learn PHP you'll see this is a common thing, one error will trigger several other errors. Because your query failed it caused none of your result set variables to be set, thus causing all the E_NOTICE level errors.

You don't need any of these lines here:
$movie_takings = NULL;
$movie_cost = NULL;
$movie_name = NULL;
$movie_year = NULL;
$director = NULL;
$leadactor = NULL;
$movie_running_time = NULL;

The why part is because you are initializing these when you use them inside of the loops:

     $movie_name = $row['movie_name'];
     $movie_director = $row['movie_director'];
     $movie_leadactor = $row['movie_leadactor'];
     $movie_year = $row['movie_year'];
     $movie_running_time = $row['movie_running_time']." mins";
     $movie_takings = $row['movie_takings'];
     $movie_cost = $row['movie_cost'];

You shouldn't be seeing errors here, each indice of the array should refer to a database field and these should always be set, except where a query fails, in which case fixing your SQL will correct all of those errors.

The E_NOTICE level errors can also alret you to when you have mispelled a variable or array indice, if you mispelled any of the fields above PHP will tell you with an E_NOTICE level error.

Make sense?

Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
Reply With Quote
  #9 (permalink)  
Old June 3rd, 2004, 06:52 PM
Authorized User
 
Join Date: May 2004
Location: , , .
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Default

When troubleshooting, you want to work backwards. First of all, the error tells you where the problem is:
Code:
while($review_row = mysql_fetch_array($review_result))
The error is telling you that $review_result is not a valid MySQL result set. Therefore, the line that creates that variable must not be doing it's job for some reason. When looking at that line:
Code:
$review_result = mysql_query($review_query,$link) or die(mysql_error());
...we see that the query is being run on $review_query, and connecting to the database specified in $link. So, if the result set is not being properly set, then either the query is not right (and is returning 0 rows), or $link is not correctly set to the database. My guess is that the link is fine, since previous connections to the database must have worked. Just in case, though, check out line 2-3:
Code:
$link = mysql_connect("localhost","root","mysqlpass")
  or die(mysql_error());

... to make sure your server, login, and password are correct.

That leaves us with the query:
Code:
$review_query = "SELECT
                         *
               FROM
                         reviews
               WHERE
                          review_movie_id ='".$_GET['movie_id']."'
               ORDER BY
                          review_date DESC";

The only part I can see that might cause the problem is if the movie_id is either the wrong id, or it's not set. If you know what movie_id you are attempting to retrieve, try hard-coding it to test it out with an id you know exists in the database:
Code:
$review_query = "SELECT * FROM reviews WHERE review_movie_id ='2' ORDER BY review_date DESC";
By this time, the error should reveal itself, I hope. If not, let us know. And be sure if you make any changes to note any changes in the error messages, and let us know what they are.

Reply With Quote
  #10 (permalink)  
Old June 3rd, 2004, 07:10 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Try echoing $review_query and seeing what it ends up being.

Snib

<><
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
undefined variable zenixcomp BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 1 October 31st, 2008 06:11 AM
How to know a variable is undefined? Edward King Javascript How-To 3 June 14th, 2005 10:14 AM
Chapter 7 check_image.php, undefined variable Ron_Bingham BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 June 3rd, 2005 10:31 PM
Undefined variable majidnakit Beginning PHP 2 April 16th, 2005 11:23 PM
undefined variable !! cody44 Beginning PHP 3 January 20th, 2005 05:47 PM



All times are GMT -4. The time now is 05:45 PM.


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