arcanepsyche Posted December 25, 2008 Report Share Posted December 25, 2008 Hello again all, and Merry Christmas! Hopefully you can help me with this small question I have about one of the PHP/MySQL video tutorials on the site. In the tutorial, Stefan uses this code to grab DB info and display it using PHP: $query = "SELECT * FROM people"; $results = mysql_query($query); $my_rows; WHILE($row = mysql_fetch_array($results, MYSQL_ASSOC)) { $my_rows=$my_rows . "Name: ($row['name']} " . "id : ($row[id]} "; } I'm not quite understanding the syntax behind the $my_rows variable. Why do we need to place it above the WHILE loop and then define+add on to it inside the WHILE loop? Thanks in advance, Zac Quote Link to comment Share on other sites More sharing options...
falkencreative Posted December 26, 2008 Report Share Posted December 26, 2008 It depends on the language you are using... some programming languages require a variable to be defined/initialized before it can be used. PHP doesn't require that though. As far as I know, the third line in your code sample above really isn't necessary, though there is nothing necessarily wrong with it. Stefan may be able to comment on this more. Quote Link to comment Share on other sites More sharing options...
administrator Posted December 26, 2008 Report Share Posted December 26, 2008 Hi, Falkencreative is correct ... I just have a habit of doing this for reasons of code clarity and because I have a history of using languages (Java etc ...) that required variable declarations and initialization. For simple operations like in this example, it is overkill, but as things get more complex, declaring variables can keep things easier to understand. Stefan Quote Link to comment Share on other sites More sharing options...
arcanepsyche Posted December 26, 2008 Author Report Share Posted December 26, 2008 Thanks guys, this was helpful! On to actually trying to build something..... Quote Link to comment Share on other sites More sharing options...
OnTarget600 Posted June 10, 2009 Report Share Posted June 10, 2009 Hello, Continuing from the code above I am getting a notice. It still outputs the results after the notice. The notice line number suggests that it is this line: $my_rows = $my_rows. "Name : {$row['name']} " . "id : {$row['id']} "; I receive this error: Notice: Undefined variable: my_rows in /srv/www/htdocs/killerphp/database.php on line 23 If I remove the second $my_rows variable the error goes away but so does the code functionality by only outputting the first db entry. I am using a standard LAMP stack with php 5.2.9 Any ideas of why and if there is replacement code? Quote Link to comment Share on other sites More sharing options...
Wickham Posted June 10, 2009 Report Share Posted June 10, 2009 You haven't shown th ecomplete code, but I think you have a { where you should have a ( in two places $my_rows = $my_rows. "Name : {$row['name']} " . "id : {$row['id']} "; should be $my_rows = $my_rows. "Name : ($row['name']} " . "id : ($row['id']} "; (as in the first post on this thread) but even that doesn't look correct, perhaps it should be:- $my_rows = $my_rows. "Name : ($row['name']) " . "id : ($row['id']) "; as { or } begin or end an instruction. Quote Link to comment Share on other sites More sharing options...
OnTarget600 Posted June 10, 2009 Report Share Posted June 10, 2009 Thanks for you quick reply. Unfortunatly it appears the curly brace "{}" are meant to be. I changed them as per your suggestion and it broke completely. Eclipse also reports a syntax error when they are changed. I wont post the error as I don't want to confuse the issue. Here is the full code. $query = "SELECT * FROM people"; $result = mysql_query($query); $my_rows; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $my_rows = $my_rows. "Name : {$row['name']} " . "id : {$row['id']} "; } echo $my_rows; Thanks again. Quote Link to comment 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.
Note: Your post will require moderator approval before it will be visible.