bnther Posted June 8, 2009 Share Posted June 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/161373-solved-undefined-variables-but-works/ Share on other sites More sharing options...
jxrd Posted June 8, 2009 Share Posted June 8, 2009 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....'; Quote Link to comment https://forums.phpfreaks.com/topic/161373-solved-undefined-variables-but-works/#findComment-851620 Share on other sites More sharing options...
bnther Posted June 8, 2009 Author Share Posted June 8, 2009 jxrd, That did it 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 Quote Link to comment https://forums.phpfreaks.com/topic/161373-solved-undefined-variables-but-works/#findComment-851631 Share on other sites More sharing options...
jxrd Posted June 8, 2009 Share Posted June 8, 2009 Np Quote Link to comment https://forums.phpfreaks.com/topic/161373-solved-undefined-variables-but-works/#findComment-851633 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.