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
https://forums.phpfreaks.com/topic/161373-solved-undefined-variables-but-works/
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....'; :)

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.