MySQL Connection and loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tommurray
    New Member
    • May 2007
    • 40

    #1

    MySQL Connection and loop

    Hi all I am new to php and i need a little help, I have a webpage which is going to draw all the content from a database were the user can login and amend the values of the field.

    Within the database I have two fields id and content. For each row I want an variable such as pageContent1 for the second row pageContent2 and so on.

    I have wrote the below code but this does not work can anyone help?

    Thank You

    Tom

    [CODE=php]
    // Make a MySQL Connection
    mysql_connect(" ###", "###", "###") or die(mysql_error ());
    mysql_select_db ("cbags") or die(mysql_error ());

    // Get all the data from the "example" table
    $result = mysql_query("SE LECT * FROM home_content")
    or die(mysql_error ());
    $h = 1;
    while($row = mysql_fetch_arr ay( $result )) {
    $pageContent[$h] = $row['content'];
    $h = $h + 1;
    }
    }
    [/CODE]
    Last edited by pbmods; Aug 23 '07, 09:30 PM. Reason: Changed [CODE] to [CODE=php].
  • jx2
    New Member
    • Feb 2007
    • 228

    #2
    i dont get it - is there anything wrong with your code?

    [php]
    // Make a MySQL Connection
    mysql_connect(" ###", "###", "###") or die(mysql_error ());
    mysql_select_db ("cbags") or die(mysql_error ());

    // Get all the data from the "example" table
    $result = mysql_query("SE LECT * FROM home_content")
    or die(mysql_error ());

    //well you can try this way (but it wont change anythinhg)
    $pageContent[0]=0;
    while($pageCont ent[] = mysql_fetch_arr ay( $result )) ;
    }
    [/php]

    ...

    Comment

    • tommurray
      New Member
      • May 2007
      • 40

      #3
      Hi thank you for getting back so quickly, in firefox my screen goes blank when uploading to my site. If I take out the code then everything loads up correctly.

      I will give what you have mentioned ago, with what you have wrote how does the pageContent1 get the content of the field in the database with an id of 1?

      Thank you

      Tom

      Comment

      • jx2
        New Member
        • Feb 2007
        • 228

        #4
        Originally posted by tommurray
        I will give what you have mentioned ago, with what you have wrote how does the pageContent1 get the content of the field in the database with an id of 1?
        eighter of them cos you used fetch_array()
        if you want to display results you can use
        [php]
        $row = implode(' , ',$pageContent[1]) );
        echo $row;
        [/php]
        that will display row 1

        Comment

        • tommurray
          New Member
          • May 2007
          • 40

          #5
          No luck still nothing gets displayed none of the design of the page displays just a blank screen.

          my original code works fine without trying to get the value pageContent to increment each time it goes through the loop.(see below)

          [php]
          // Make a MySQL Connection
          mysql_connect(" mysql", "webuser", "pa55w0rd") or die(mysql_error ());
          mysql_select_db ("cbags") or die(mysql_error ());

          // Get all the data from the "example" table
          $result = mysql_query("SE LECT content FROM home_content WHERE id = 1")
          or die(mysql_error ());

          while($row = mysql_fetch_arr ay( $result )) {
          $pageContent = $row['content'];
          }
          [/php]

          Comment

          • jx2
            New Member
            • Feb 2007
            • 228

            #6
            Originally posted by tommurray
            No luck still nothing gets displayed none of the design of the page displays just a blank screen.

            my original code works fine without trying to get the value pageContent to increment each time it goes through the loop.(see below)

            Code:
            // Make a MySQL Connection
            mysql_connect("mysql", "webuser", "pa55w0rd") or die(mysql_error());
            mysql_select_db("cbags") or die(mysql_error());
            
            // Get all the data from the "example" table
            $result = mysql_query("SELECT content FROM home_content WHERE id = 1") 
            or die(mysql_error());  
            
            while($row = mysql_fetch_array( $result )) {
            	$pageContent = $row['content'];
            }
            as i wrote above you need to use echo

            Comment

            • tommurray
              New Member
              • May 2007
              • 40

              #7
              Originally posted by jx2
              as i wrote above you need to use echo
              I have used echo on the example you provided but it did not work nothing was displayed.

              Comment

              • jx2
                New Member
                • Feb 2007
                • 228

                #8
                Originally posted by tommurray
                I have used echo on the example you provided but it did not work nothing was displayed.
                try to use
                [php]
                echo mysql_error();
                [/php]
                you might have error in your sql query
                or you display wrong variable

                Comment

                • jx2
                  New Member
                  • Feb 2007
                  • 228

                  #9
                  [php]
                  mysql_connect(" mysql", "webuser", "pa55w0rd") or die(mysql_error ());
                  mysql_select_db ("cbags") or die(mysql_error ());

                  // Get all the data from the "example" table
                  $result = mysql_query("SE LECT content FROM home_content WHERE id = 1")
                  or die(mysql_error ());

                  while($row = mysql_fetch_arr ay( $result )) {
                  foreach($row as $v){
                  echo "$v , ";
                  }
                  echo "\n<br />";

                  }
                  echo mysql_error();
                  [/php]

                  try this one

                  Comment

                  • tommurray
                    New Member
                    • May 2007
                    • 40

                    #10
                    I have added in the echo for the mysql error, but nothing is displayed still.

                    BTW thank you for your help

                    I now have the script as:

                    [php]// Make a MySQL Connection
                    mysql_connect(" mysql", "webuser", "pa55w0rd") or die(mysql_error ());
                    mysql_select_db ("cbags") or die(mysql_error ());

                    // Get all the data from the "example" table
                    $result = mysql_query("SE LECT * FROM home_content")
                    or die(mysql_error ());

                    $pageContent[0]=0;
                    while($pageCont ent[] = mysql_fetch_arr ay( $result )) ;
                    $row = implode(' , ',$pageContent[1]) );
                    echo $row;
                    echo mysql_error();
                    }[/php]

                    Comment

                    • tommurray
                      New Member
                      • May 2007
                      • 40

                      #11
                      Originally posted by jx2
                      [php]
                      mysql_connect(" mysql", "webuser", "pa55w0rd") or die(mysql_error ());
                      mysql_select_db ("cbags") or die(mysql_error ());

                      // Get all the data from the "example" table
                      $result = mysql_query("SE LECT content FROM home_content WHERE id = 1")
                      or die(mysql_error ());

                      while($row = mysql_fetch_arr ay( $result )) {
                      foreach($row as $v){
                      echo "$v , ";
                      }
                      echo "\n<br />";

                      }
                      echo mysql_error();
                      [/php]

                      try this one
                      Progress I know get the page loading up with the following line:

                      1 , 1 , Next Event , Next Event ,

                      so it is writing out two id's and two of the content field for the one row.

                      Comment

                      • tommurray
                        New Member
                        • May 2007
                        • 40

                        #12
                        Thank you for your help I have solved my problem by changing to the following:
                        [PHP]

                        while($row = mysql_fetch_arr ay( $result )) {
                        foreach($row as $v){
                        $pageContent[$v] = $row['content'];
                        }
                        }

                        [/PHP]

                        Then Further down in my page i have

                        [PHP]<?php echo $pageContent[1]; ?>[/PHP]

                        replacing the 1 to a 2 for the second row of content.

                        Regards

                        Tom

                        Comment

                        • jx2
                          New Member
                          • Feb 2007
                          • 228

                          #13
                          1 , 1 , Next Event , Next Event ,
                          well you neet to use mysql_fetch_ass oc() or mysql_fetch_row () if you want to avoid this problem

                          what you did is actualy bad and i woudnt encurage you do do that

                          regards
                          jx2

                          Comment

                          • tommurray
                            New Member
                            • May 2007
                            • 40

                            #14
                            Which Part is not encouraged an what way to do it would be encouraged?

                            Tom

                            Comment

                            Working...