Tip:
Highlight text to annotate it
X
So again, we selected our database here and then we ran a query. So we do that by using
the function mysql underscore query() and then we passed it the query. So we created
a number of queries like this in PHPMyAdmin, none of them were quite this simple but the
language is the same. So anything that works in PHPMyAdmin in that query window will also
work for the mysql_query() function. But this returns as an object, and we can't do much
with that object directly, but we can use other functions in order to pull out the individual
rows and do something with that data.
So in this next step we begin with an output variable that's empty, that's where we're
going to store our output, and then we loop through each row of our result by using a
function called mysql_fetch_array(). And the one parameter we pass to it is the result
that we got back from mysql_query().
What mysql_fetch_array() will do is grab a row from our result set and put it inside
of this variable here, we're calling it row, and that row will contain an array of all
of the information that we returned from our query. What mysql_fetch_array() will also
do is move a pointer to the next row so the next time this runs, you see that this is
in a while statement, it will grab the next row in the result set. Now, that pointer idea
is an invisible thing but it's important to understand that we don't have to move anything
forward, that this mysql_fetch_array() will automatically go to the next row every time
it runs. And when it gets to the end of the rows, it will actually return false.
So when we say that row equals false, row will equal false. And this while statement
will end because as you know from previous videos while will only run while this condition
inside is true. So this is a lot like how we used foreach in order to loop through an
array. Every time foreach ran, it would grab the element from that item in the array and
then move a pointer to the next element. So it will run foreach through every item and
then stop once we got to the end.
Within this while loop, we're using a foreach loop to loop through each item in the row
array, and we're pulling out the key and values so we can display them. And then we're adding
the key and the value to the output on a new line. So we say that the key equals the value
and then we add a break. And we add one additional break at the end of each row in order to separate
the records. And finally, once we put all these together, we go ahead and print it out.
So if you go back to the browser and we refresh, you can see what's inside of each row that
gets returned from my mysql_fetch_array(). We have a key of zero that has the value of
George, a key of name that also has a value of George, a key of one that has a value of
ten, and a key of shoe_size that has a value of ten.
Now, you see this number increment. It goes from zero to one to two to three, and this
number gives us one way to access the data within the row. But we can also access the
data by using the column name. At the end of one record, you see we have this break
and then we begin with Sally and you see that the keys are all identical here and same with
Deepak.