...

View Full Version : Resolved MySQL Row loop



roboticsguy1988
09-21-2009, 05:49 AM
Ok... So let me explain what I am trying to do. I have a selection box on a form for people to select which university they are from. The name of each university should come from a table called universities and a field called UNIVERSITY. So if i have 10 universities in the table it prints all 10 selections and if i have only 5 universities it only prints 5 universities.

So the following is the code i have came up with. I already have the form code all typed and everything. All of the form code is in one big echo statement for several purposes. Anyways this code is in the mid part of the form and i have already connected to the database.

It seems however when i run this code i get no results everything is blank, and i do have data in the table.



<select name=\"university\" id=\"university\">
<?php
$result = mysql_query(\"SELECT * FROM universities\");

while ($row = mysql_fetch_assoc($result))
{
echo '<option>';
echo {$row['UNIVERSITY']};
echo '</option>';
}
?>
</select>

kbluhm
09-21-2009, 07:18 AM
Lots of errors there... you appear to be escaping every double-quote, regardless of scope... ?? Also, curly braces surrounding a variable are not necessary unless already within a double-quoted string. I'll bet if you view the source code you'll find errors hidden within the select-tag.



<select name="university" id="university">
<?php
$result = mysql_query("SELECT * FROM universities");

while ($row = mysql_fetch_assoc($result))
{
echo '<option>';
echo $row['UNIVERSITY'];
echo '</option>';
}
?>
</select>

Coyote6
09-22-2009, 02:19 AM
Only select the fields you need in your query.



<select name="university" id="university">
<?php
$result = mysql_query("SELECT UNIVERSITY FROM universities");
while ($row = mysql_fetch_assoc($result)) {
echo "<option>{$row['UNIVERSITY']}</option>";
}
?>
</select>


This will make your page faster and is better practice.

roboticsguy1988
09-22-2009, 02:31 AM
Coyote:
When I use your code I get no errors, but also nothing prints.

Kbluhm:
If I don't include the curly braces I get the following error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

It seems when i take the form out of the echo statement it is in that everything works fine... which is odd... I have run other php queries within an echo statement before and they work fine. Would using print instead of echo have any affect?

Any suggestions would be very helpful and I would be very grateful.

kbluhm
09-22-2009, 03:02 AM
Kbluhm:
If I don't include the curly braces I get the following error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
Whose example are you using? Post all the code involved in producing this error.

roboticsguy1988
09-22-2009, 04:28 AM
Whose example are you using? Post all the code involved in producing this error.

When i use this code...



<select name=\"university\" id=\"university\">
<?php
$result = mysql_query(\"SELECT * FROM universities\");

while ($row = mysql_fetch_assoc($result))
{
echo '<option>';
echo $row['UNIVERSITY'];
echo '</option>';
}
?>
</select>


I get the following error...

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

Then when I use this code:


<select name=\"university\" id=\"university\">
<?php
$result = mysql_query(\"SELECT UNIVERSITY FROM universities\");
while ($row = mysql_fetch_assoc($result)) {
echo \"<option>{$row['UNIVERSITY']}</option>\";
}
?>
</select>


I receive no errors but the data also does not print out.

Also as a side nto i have do escape out of the double braces as i am INSIDE an echo statement for all of my HTML / PHP code.

Keep in mind that if i take the whole form out of the echo statement and use it at the bottom of the page in regular HTML form then it works just fine.

roboticsguy1988
09-22-2009, 04:41 AM
Hmmm well despite all the effort I figured out a way to make it work without everything being in the echo statement. So thanks for all your guys help!

kbluhm
09-22-2009, 04:42 AM
Despite all the effort? Next time post your entire code, and this would have been an open and shut case. You cannot break into PHP when already within PHP. Did you ever view the output source code like I'd suggested?

seco
09-22-2009, 04:47 AM
dont forget the option value=

roboticsguy1988
09-22-2009, 04:49 AM
dont forget the option value=

Ah yes, thank you much kind sir!

hinch
09-22-2009, 10:35 AM
Despite all the effort? Next time post your entire code, and this would have been an open and shut case. You cannot break into PHP when already within PHP. Did you ever view the output source code like I'd suggested?

and he still didn't listen about escaping double quotes within the wrong scopes :)

jegan2k9
09-22-2009, 01:01 PM
Hi ,

Please try this,

<select name="university" id="university">
<?php
$result = mysql_query("SELECT * FROM universities");

while ($row = mysql_fetch_array($result))
{
?>
<option value="<?php echo $row['UNIVERSITY'];?>"><?php echo $row['UNIVERSITY']; ?>
</option>
<?php
}
?>
</select>

roboticsguy1988
09-22-2009, 02:48 PM
Despite all the effort? Next time post your entire code, and this would have been an open and shut case. You cannot break into PHP when already within PHP. Did you ever view the output source code like I'd suggested?

I meant despite all my efforts. Sometimes users don't want to post all of there code as there is some code that some people may not want others to see. I said quite a few times throughout the post that everything was in an echo statement, if you can't read i'm sorry. Just for fyi if i removed the php tags it still did not print anything.


and he still didn't listen about escaping double quotes within the wrong scopes :)

You know i am finding that i have to mention things more than once on this forum. It seems none of you can read. If you are INSIDE an ECHO statement like my ENTIRE form was then if you have any quotes you have to use \" or '. This is common PHP knowledge.


Hi ,

Please try this,

<select name="university" id="university">
<?php
$result = mysql_query("SELECT * FROM universities");

while ($row = mysql_fetch_array($result))
{
?>
<option value="<?php echo $row['UNIVERSITY'];?>"><?php echo $row['UNIVERSITY']; ?>
</option>
<?php
}
?>
</select>

Thanks
www.joomla-web-designer.com
Professional Joomla Services,Joomla Website Maintenance, Joomla troubleshooting, Joomla E commerce

www.joomla-web-developer.com
Joomla Programmers, Joomla component development ,Joomla theme design,Joomla Maintenance

This ended up being the solution i took my whole form out of the echo statement and only used PHP where needed.

So for everyone's knowledge the problem is solved, thank you everyone for your help. No further assistance is needed.

kbluhm
09-22-2009, 03:30 PM
I meant despite all my efforts. Sometimes users don't want to post all of there code as there is some code that some people may not want others to see. I said quite a few times throughout the post that everything was in an echo statement, if you can't read i'm sorry. Just for fyi if i removed the php tags it still did not print anything.

Once again, post all of your code next time. Without it we are simply grabbing at straws. I have seen plenty of times on here where someone says their code is doing one thing, but when they post it it turns out they have copied and pasted it from a tutorial, or some outside resource, and simply do not understand what it is doing. The code operates as it was written, and when I see no "echo" up front, I have to assume it is not there. And even if it is, what type of string is it? Is it a single-quoted string? A double-quoted string? Are you using Heredoc syntax? No one knows if you don't post all of your code.

MattF
09-22-2009, 03:41 PM
You know i am finding that i have to mention things more than once on this forum. It seems none of you can read. If you are INSIDE an ECHO statement like my ENTIRE form was then if you have any quotes you have to use \" or '. This is common PHP knowledge.

Have you seen how many posts are made on the board? People scan the code primarily rather than the exact post content due to the sheer volume of content.

If you are posting regarding a coding error, why should people naturally assume that any of your code is correct?

roboticsguy1988
09-22-2009, 04:58 PM
Have you seen how many posts are made on the board? People scan the code primarily rather than the exact post content due to the sheer volume of content.

If you are posting regarding a coding error, why should people naturally assume that any of your code is correct?

Btw, with a response like that to people who have tried helping you, (in their own time and for free, might I add), I am sure that peoples reading skills will become far worse with regards to your future posts.


Once again, post all of your code next time. Without it we are simply grabbing at straws. I have seen plenty of times on here where someone says their code is doing one thing, but when they post it it turns out they have copied and pasted it from a tutorial, or some outside resource, and simply do not understand what it is doing. The code operates as it was written, and when I see no "echo" up front, I have to assume it is not there. And even if it is, what type of string is it? Is it a single-quoted string? A double-quoted string? Are you using Heredoc syntax? No one knows if you don't post all of your code.

And one last thing, apologizing to me for my inability to read is quite childish and insulting, and I hope you learn to treat others better the next time they take time out of their day to help you figure out your own programming shortcomings for free. I'll guarantee you the next time you try to nest PHP tags and then post 5 lines of code, no help will be coming from me. ;)

I did not mean to insult anyone or give the impression that i was not thankful. I am thankful for all of the help that everyone has gave me. I was simply stating that I posted several times that this whole thing was within a form which was within a php echo statement. It is common practice that within an echo statement you escape quotes or use single quotes (Reference http://webdevjunk.com/coding/php/31/tired-of-escaping-quotes-in-your-php-echo-statements/).

As far as posting ALL of my code i can certainly do that if you want 5 pages worth of code. Sorry if that was really a big deal i felt that i explained it well enough. If the rest of the form works fine and the rest of the page works fine than most likely it is usually just a problem with the small portion of code.

I am sorry for my previous posts, and once again as i have also said many times Thank you ALL for the help.

JohnDubya
09-22-2009, 05:19 PM
Guys, keep it on topic, or else use PM's. This is your first and only warning.

Rule 1.1 - http://www.codingforums.com/rules.htm



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum