ebz451 Posted May 1, 2012 Share Posted May 1, 2012 Hey Everyone, I writing on here because I am taking a course on PHP and learning it by using the textbook PHP 6 Fast & Easy Web Development by Julie Meloni and came across a problem on an assignment that I dont know how to fix. Any who, in the assignment I was working on I wrote a script that shows data I have in a table in my broswer (which btw works) BUT it always gives me the following message: Notice: Undefined variable: display_block in 127.0.0.1\sel_byid.php on line 29. I have checked my code over and and am sure I copied it right but still get the notice. Here's the code: <?php $db_name = "testDB"; $table_name = "my_music"; $connection = @mysql_connect("localhost", "brenda", "mouse") or die(mysql_error()); $db = @mysql_select_db($db_name, $connection) or die(mysql_error()); $sql = "SELECT * FROM $table_name ORDER BY title"; $result = @mysql_query($sql, $connection) or die(mysql_error()); while($row = mysql_fetch_array($result)) { $id = $row['id']; $format = $row['format']; $title = stripslashes($row['title']); $artist_fn = stripslashes($row['artist_fn']); $artist_ln = stripslashes($row['artist_ln']); $rec_label = stripslashes($row['rec_label']); $my_notes = stripslashes($row['my_notes']); $date_acq = $row['date_acq']; if($artist_fn != "") { $artist_fullname = trim("$artist_fn $artist_ln"); }else{ $artist_fullname = trim("$artist_ln"); } if($date_acq == "0000-00-00") { $date_acq = "[unknown]"; } $display_block .= "<P><strong>$id. $title</strong>on $rec_label, by $artist_fullname<br>$my_notes<em>(acquired: $date_acq, format: $format)</em></P>"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>My Music (Ordered by Title)</title> </head> <body> <H1>My Music: Ordered by Title</H1> <?php echo "$display_block"; ?> <P><a href="my_menu.html">Return to Menu</a></P> </body> </html> I've been trying to fix it for like a week now and the only thing I understand from the notice is that this line: $display_block .= "<P><strong>$id. $title</strong>on $rec_label, by $artist_fullname<br>$my_notes<em>(acquired: $date_acq, format: $format)</em></P>"; is trying to concatentate to a variable which does not exist ($display_block) and I dont know how to fix it... Can anyone help me? Quote Link to comment https://forums.phpfreaks.com/topic/261916-how-can-i-fix-this-undefined-variable-in-my-code/ Share on other sites More sharing options...
floridaflatlander Posted May 1, 2012 Share Posted May 1, 2012 You can try the poor mans way, $display_block = FALSE; somewhere about the var in question. I'd put it above $db_name Quote Link to comment https://forums.phpfreaks.com/topic/261916-how-can-i-fix-this-undefined-variable-in-my-code/#findComment-1342074 Share on other sites More sharing options...
wigwambam Posted May 1, 2012 Share Posted May 1, 2012 Simply define the variable at the top, above $db_name : $display_block = ''; Quote Link to comment https://forums.phpfreaks.com/topic/261916-how-can-i-fix-this-undefined-variable-in-my-code/#findComment-1342076 Share on other sites More sharing options...
xyph Posted May 1, 2012 Share Posted May 1, 2012 Bad advice, edited out. Quote Link to comment https://forums.phpfreaks.com/topic/261916-how-can-i-fix-this-undefined-variable-in-my-code/#findComment-1342081 Share on other sites More sharing options...
ebz451 Posted May 1, 2012 Author Share Posted May 1, 2012 Thanks for your replies guys, here's what I did with your advice: I'm going to try this right now... this is the same as writing $display_block = ''; right? You can try the poor mans way, $display_block = FALSE; somewhere about the var in question. I'd put it above $db_name Just tried this and it makes the notice go away but the script only shows one data entry from the table (the old script shows the notice and all data entries in the table)... Simply define the variable at the top, above $db_name : $display_block = ''; Thats what I was thinking but I dont know how to edit it out without using an echo command... Bad advice, edited out. Quote Link to comment https://forums.phpfreaks.com/topic/261916-how-can-i-fix-this-undefined-variable-in-my-code/#findComment-1342093 Share on other sites More sharing options...
xyph Posted May 1, 2012 Share Posted May 1, 2012 You want to define your variable before the loop. Quote Link to comment https://forums.phpfreaks.com/topic/261916-how-can-i-fix-this-undefined-variable-in-my-code/#findComment-1342105 Share on other sites More sharing options...
ebz451 Posted May 1, 2012 Author Share Posted May 1, 2012 Got it, thanks. I ended up defining it as a null Quote Link to comment https://forums.phpfreaks.com/topic/261916-how-can-i-fix-this-undefined-variable-in-my-code/#findComment-1342122 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.