Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: MySQL Row loop

  1. #1
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    MySQL Row loop

    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.

    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>
    Last edited by roboticsguy1988; 09-22-2009 at 04:56 PM.

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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.

    PHP 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>
    Last edited by kbluhm; 09-21-2009 at 06:22 AM.

  • Users who have thanked kbluhm for this post:

    roboticsguy1988 (09-22-2009)

  • #3
    Regular Coder
    Join Date
    May 2009
    Location
    Moore, OK
    Posts
    282
    Thanks
    11
    Thanked 41 Times in 41 Posts
    Only select the fields you need in your query.

    PHP 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>
    This will make your page faster and is better practice.

  • Users who have thanked Coyote6 for this post:

    roboticsguy1988 (09-22-2009)

  • #4
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by roboticsguy1988 View Post
    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.

  • #6
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by kbluhm View Post
    Whose example are you using? Post all the code involved in producing this error.
    When i use this code...

    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:
    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.
    Last edited by roboticsguy1988; 09-22-2009 at 03:31 AM.

  • #7
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Smile

    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!

  • #8
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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?

  • #9
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    dont forget the option value=

  • Users who have thanked seco for this post:

    roboticsguy1988 (09-22-2009)

  • #10
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by seco View Post
    dont forget the option value=
    Ah yes, thank you much kind sir!

  • #11
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    Quote Originally Posted by kbluhm View Post
    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
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #12
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    MySQL Row loop

    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>
    Last edited by JohnDubya; 09-22-2009 at 04:14 PM. Reason: Removed self-promotion links

  • #13
    New to the CF scene
    Join Date
    Sep 2009
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by kbluhm View Post
    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.

    Quote Originally Posted by hinch View Post
    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.

    Quote Originally Posted by jegan2k9 View Post
    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.

  • #14
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts

    Thumbs down

    Quote Originally Posted by roboticsguy1988 View Post
    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.
    Last edited by JohnDubya; 09-22-2009 at 04:16 PM. Reason: Removed personal comments

  • #15
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by roboticsguy1988 View Post
    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?
    Last edited by JohnDubya; 09-22-2009 at 04:17 PM. Reason: Removed personal comments


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •