Jump to content

Questions from a mysql tutorial video.


arcanepsyche

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 5 months later...

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...