Since no array exists in the database, what does the fetch array command actually do?

It obviously gets all the field names and corresponding values from a database. But is one then responsible for creating an array?

Such as something along the lines of:

echo "

$colors[0] = 'blue'
$colors[1] = 'green'
$colors[2] = 'purple'

";

If so, I am unsure why it is called fetch array. Seems like it could be called or one could use, fetch row, adding a loop and the array construction above.

I am guessing the column name is considered the array key, with the value under that column the array value.

All of my books show how to build arrays. But none of them explains the array/database relationship.

    It obviously gets all the field names and corresponding values from a database.

    No, it gets all the field names and corresponding values from the current row in the result set. However, note that it contains duplicates, i.e., the array result of mysql_fetch_assoc() and mysql_fetch_row().

    But is one then responsible for creating an array?

    No, it returns an array for you.

      laserlight wrote:

      No, it gets all the field names and corresponding values from the current row in the result set. However, note that it contains duplicates, i.e., the array result of mysql_fetch_assoc() and mysql_fetch_row().

      No, it returns an array for you.

      Oh wow, my thread ended up appearing. I left earlier after waiting for it to post and finding nothing.

      Anyway, you are saying it returns an array, yet it seems to me like the coder must build an array with an echo statement in order to see anything. Otherwise, he is left with merely column-name / value matches. Those, I guess, could be seen as a series of arrays, one per column name,

        You may want to take a look at the [man]mysql_fetch_assoc/man page for an example of how to use these mysql_fetch_*() type functions.

          12 days later

          The array being fetched is an array of the values in a row.

          $result=mysql_query("SELECT name, address, phone FROM person WHERE name LIKE 'John%'");
          while($row=mysql_fetch_array($result)){
          //$row is an array containing the results of the row
          //the array contains NAMED elements
          echo $row['name'].' '.$row['address'].' '.$row['phone'];
          //also NUMBERED elements -- in the order SELECTed
          echo "$row[0] $row[1] $row[2]";
          }
          

          in the example above both statements:
          echo $row['name'].' '.$row['address'].' '.$row['phone'];
          echo "$row[0] $row[1] $row[2]";
          will echo EXACTLY THE SAME RESULTS, like
          Joe 123 Anystreet 222-2222
          Joe 123 Anystreet 222-2222

            And if by "array" you mean an array of all the results returned, then yes, it is your job to build it.

            You have a table, and you're selecting (some of) its rows. Each row contains some number of fields. That's a two-dimensional structure: an array of arrays. Each call of fetch_array() returns an array that represents one row: a one-dimensional structure. If you want the second dimension then you put each of those rows into an array as you fetch them.

            randalusa wrote:

            the coder must build an array with an echo statement in order to see anything.

            Well, yes. Not every fetching of a row from a database has to be visible. The programmer might want to do something else with the data.

            Otherwise, he is left with merely column-name / value matches.

            Represented as an array.

            But as the others have already strongly hinted: the best way to understand what it claims to do and what it actually does is to actually play with it for a bit and see for yourself.

              7 days later
              nemonoman wrote:

              The array being fetched is an array of the values in a row.

              $result=mysql_query("SELECT name, address, phone FROM person WHERE name LIKE 'John%'");
              while($row=mysql_fetch_array($result)){
              //$row is an array containing the results of the row
              //the array contains NAMED elements
              echo $row['name'].' '.$row['address'].' '.$row['phone'];
              //also NUMBERED elements -- in the order SELECTed
              echo "$row[0] $row[1] $row[2]";
              }
              

              in the example above both statements:
              echo $row['name'].' '.$row['address'].' '.$row['phone'];
              echo "$row[0] $row[1] $row[2]";
              will echo EXACTLY THE SAME RESULTS, like
              Joe 123 Anystreet 222-2222
              Joe 123 Anystreet 222-2222

              My apologies. I disappeared again. I go through days occasionally unwilling to bother except for maybe reviewing a few pages in the book or a video. Took a look here just now. The two newest replies are quite helpful in helping me more firmly cement the idea of how an array is built from table column names and values.

              How weird to wake up last week realizing that after all my studying, I had never figured out where precisely all of these arrays were going to come from. In the books, they just build 'em on the spot (page) instead of pulling them out of tables (to a large extent). Thinking back, I guess they do build some out of the table. But the many lessons of them being built on the page right before the echo command made it all get hazy.

              So, thanks, nemonoman

              And thanks, Weedpacket

              Your contributions were not wasted.

                Write a Reply...