Jump to content

How can I fix this undefined variable in my code?


ebz451

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.