assign db fields to variables with dynamically built var names

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • webandwe
    New Member
    • Oct 2006
    • 142

    assign db fields to variables with dynamically built var names

    Hi,

    I need to store database data in variables so I can work with it.
    Please se below what I am trying to do in my script....

    If you echo the stuff out is work fine. Meaning if I go echo $row['a5]; I get the data I want but I can't get the data to store in a vairable.

    I need something that goes from $a1 to $a18 and in each of then store the data from one field in the database name a5.

    Thanks

    [PHP]
    <?php
    $con = mysql_connect(" localhost","*** **","******") ;
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }

    mysql_select_db ("*******", $con);

    $result = mysql_query("SE LECT * FROM hole ORDER BY a5");

    while($row = mysql_fetch_arr ay($result))
    {
    $a1 = $row['a5'];
    $a2 = $row['a5']; /////// needs to give second result
    $a3 = $row['a5']; /////// needs to give third result

    }

    echo "<img name=\"Hole\" src=\"$a1.jpg\" width=\"32\" height=\"32\" alt=\"Hole\">";



    mysql_close($co n);


    ?>
    [/PHP]
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Please remember to provide a meaningful Title for any threads started (see the FAQ entry Use a Good Thread Title).

    This helps to ensure that other members, and also the general public, will have a better chance of finding answers to any similar questions.

    MODERATOR

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Originally posted by webandwe
      Hi,

      I need to store database data in variables so I can work with it.
      Please se below what I am trying to do in my script....

      If you echo the stuff out is work fine. Meaning if I go echo $row['a5]; I get the data I want but I can't get the data to store in a vairable.

      I need something that goes from $a1 to $a18 and in each of then store the data from one field in the database name a5.

      Thanks

      [PHP]
      <?php
      $con = mysql_connect(" localhost","*** **","******") ;
      if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

      mysql_select_db ("*******", $con);

      $result = mysql_query("SE LECT * FROM hole ORDER BY a5");

      while($row = mysql_fetch_arr ay($result))
      {
      $a1 = $row['a5'];
      $a2 = $row['a5']; /////// needs to give second result
      $a3 = $row['a5']; /////// needs to give third result

      }

      echo "<img name=\"Hole\" src=\"$a1.jpg\" width=\"32\" height=\"32\" alt=\"Hole\">";



      mysql_close($co n);


      ?>
      [/PHP]
      This is untested (just some logic)
      [php]
      $_i = 0;
      $_a = array();
      while($_row = mysql_fetch_arr ay($_res))
      {
      $_a[$_i] = $_row['col'];
      ++$_i;
      }
      [/php]

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Your question is so not clear!

        Do you want

        1. to assign the content of column 'a5' in each subsequent row into a variable $a1

        2. store the contents of column 'a5' of each row into 18 different variables starting with $a1-$a18?

        3. store the content of column 'a5' in variables $a1-$a18, e.g. column 'a5' content from row 1 goes into $a1, from row 10 goes into $a10, from row 18 goes into $a18.

        Please describe your problem a bit more clearer.

        Ronald

        Comment

        • webandwe
          New Member
          • Oct 2006
          • 142

          #5
          Originally posted by ronverdonk
          Your question is so not clear!

          Do you want

          1. to assign the content of column 'a5' in each subsequent row into a variable $a1

          2. store the contents of column 'a5' of each row into 18 different variables starting with $a1-$a18?

          3. store the content of column 'a5' in variables $a1-$a18, e.g. column 'a5' content from row 1 goes into $a1, from row 10 goes into $a10, from row 18 goes into $a18.

          Please describe your problem a bit more clearer.

          Ronald

          Hi yes.

          Number 3.

          3. store the content of column 'a5' in variables $a1-$a18, e.g. column 'a5' content from row 1 goes into $a1, from row 10 goes into $a10, from row 18 goes into $a18.

          Please describe your problem a bit more clearer.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by webandwe
            Hi yes.

            Number 3.

            3. store the content of column 'a5' in variables $a1-$a18, e.g. column 'a5' content from row 1 goes into $a1, from row 10 goes into $a10, from row 18 goes into $a18.

            Please describe your problem a bit more clearer.
            Then my post should suffice.

            Comment

            • webandwe
              New Member
              • Oct 2006
              • 142

              #7
              I am testing it but for some reason it does not want to go...

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by webandwe
                I am testing it but for some reason it does not want to go...
                Post what youre using

                Comment

                • webandwe
                  New Member
                  • Oct 2006
                  • 142

                  #9
                  [PHP]
                  <?php
                  $con = mysql_connect(" l*****","*****" ,"*****");
                  if (!$con)
                  {
                  die('Could not connect: ' . mysql_error());
                  }

                  mysql_select_db ("****datebase* ****", $con);

                  $result = mysql_query("SE LECT * FROM hole ORDER BY a5");

                  $i = 0;
                  $a = array();
                  while($row = mysql_fetch_arr ay($result))
                  {
                  $a[$i] = $row['a5'];
                  ++$i;
                  }

                  mysql_close($co n);
                  ////Testing
                  echo "$a1 $a2 $a3 ";

                  ?>
                  [/PHP]

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    Originally posted by webandwe
                    [PHP]
                    <?php
                    $con = mysql_connect(" l*****","*****" ,"*****");
                    if (!$con)
                    {
                    die('Could not connect: ' . mysql_error());
                    }

                    mysql_select_db ("****datebase* ****", $con);

                    $result = mysql_query("SE LECT * FROM hole ORDER BY a5");

                    $i = 0;
                    $a = array();
                    while($row = mysql_fetch_arr ay($result))
                    {
                    $a[$i] = $row['a5'];
                    ++$i;
                    }

                    mysql_close($co n);
                    ////Testing
                    echo "$a1 $a2 $a3 ";

                    ?>
                    [/PHP]
                    Remember, the variables are being assigned into an array - not seperate variables.
                    [php]
                    echo "{$_a[0]} {$_a[1]} {$_a[2]}";
                    [/php]

                    Comment

                    • webandwe
                      New Member
                      • Oct 2006
                      • 142

                      #11
                      Super, it is working.!!!!!!! !!!!!!!

                      Never worked with it before but now i'll know!

                      Thanks a mil again!

                      Comment

                      • ronverdonk
                        Recognized Expert Specialist
                        • Jul 2006
                        • 4259

                        #12
                        Originally posted by markusn00b
                        Remember, the variables are being assigned into an array - not seperate variables.
                        [php]
                        echo "{$_a[0]} {$_a[1]} {$_a[2]}";
                        [/php]
                        That is cheating: the requirement was to assign them to separate variables $a1 - $a18. You can do that as follows:
                        [php]while($row = mysql_fetch_arr ay($result))
                        {
                        $var='a'.$i;
                        $$var = $row['a5'];
                        ++$i;
                        }
                        mysql_close($co n);
                        ////Testing
                        echo "$a1 $a2 $a3 ";[/php]Ronald

                        Comment

                        • Markus
                          Recognized Expert Expert
                          • Jun 2007
                          • 6092

                          #13
                          Originally posted by webandwe
                          Super, it is working.!!!!!!! !!!!!!!

                          Never worked with it before but now i'll know!

                          Thanks a mil again!
                          Welcome!
                          Regards.
                          :)

                          Comment

                          • webandwe
                            New Member
                            • Oct 2006
                            • 142

                            #14
                            Originally posted by ronverdonk
                            That is cheating: the requirement was to assign them to separate variables $a1 - $a18. You can do that as follows:
                            [php]while($row = mysql_fetch_arr ay($result))
                            {
                            $var='a'.$i;
                            $$var = $row['a5'];
                            ++$i;
                            }
                            mysql_close($co n);
                            ////Testing
                            echo "$a1 $a2 $a3 ";[/php]Ronald

                            This one work the best for me because I need to use the Vairables all over the place. Thanks!!!!!

                            Comment

                            • ronverdonk
                              Recognized Expert Specialist
                              • Jul 2006
                              • 4259

                              #15
                              As Markus already said: you are welcome!. See ya again.

                              Ronald

                              Comment

                              Working...