Jump to content

[SOLVED] Undefined variables --- but works?


bnther

Recommended Posts

I'm following along a video tutorial for a very basic SELECT.  I run the code and it returns everything that I have put in my table.  The only catch is I get this error: Notice: Undefined variable: my_rows   You'll see in the code that my_rows is declared, but there's nothing to define it.  I'm used to Java where you always declare variable type so this is the first thing that grabbed at my attention.  However, it was not defined in the tutorial so maybe that's not how things roll in PHP'ville.

 

mysql_select_db('chamber');

$query = "SELECT * FROM announcements";
$result = mysql_query($query);
$my_rows;


while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
	$my_rows = $my_rows . "{$row['text']}"; /* this is the line that's throwing the notice*/
}

 

Any light on this subject would be appreciated.  :)

Link to comment
Share on other sites

Try this

   mysql_select_db('chamber');
   
   $query = "SELECT * FROM announcements";
   $result = mysql_query($query);
   $my_rows = NULL;
   
   
   while($row = mysql_fetch_array($result, MYSQL_ASSOC))
   {
      $my_rows .= "{$row['text']}";
   }

 

You need to actually assign a value to $my_rows before you can append to it, even it's just a null value. Note the use of .= to append - quicker than $my_rows = $my_rows .'blah....'; :)

Link to comment
Share on other sites

jxrd,

 

That did it  ;D

I was kind of wondering about the my_rows all by itself.  I didn't realize that you could put a NULL value to it though.  That was new.  Also, thank you for the ' .= ' vs writing it all out.  Shortcuts like that keep me from keying in things incorrectly. 

 

Many thanks  :)

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.