Inserting Database Results Into Array in PHP

in the last episode we talked about how to take data and put it inside an empty array in this episode we're going to talk about take data from a database and insert it into an array so you can actually use it for later as you guys can see already have a couple of documents here with some stuff in it so before we get started I'd like to explain what exactly did so far inside my index page i have included a database connection at the very top the database connection is right here inside my other page and it's just a very basic connection to my localhost database which is called test now inside my test database as you guys can see I have one table called data and inside my data table have a couple of different rows of data i just have a very basic ID for each data and also have some basic text right now he's as you guys can see each row has a just one word hi there friend you seem like a cool person now what I'd like to do is I'd like to take this data and put it inside an array so I want to have a radially spits out one row of data I want to have an array of spits out all the text data we have going on vertically now if we go back inside my index page two guys consider inside my body tag also wrote some code now let did here was a random basic select statements i quoted inside my database and then afterwards or create an empty array now you don't actually have to create this empty array but a habit i recommend you guys do it now I chose to call this array datos which is my way of writing data in plural which i know is not correct but just for the sake of this exercise i call it datas now underneath my mt are a Miranda if statement that said ok if we have more than see results from this select statement which means that if we have any kind of data running to select statements then is to run this loop down here that is just a very basic wildest okay while we have results from the database spit it out or at least do something with it so inside my while statement i just did like we didn't last episode where I inserted my data from the database with right now as you guys can see are called dollar sign rho is equal to the actual database results i went ahead and took those results from dollar sign row and put it inside my data array so right now our data array is full of data results from the database so how do we actually get this out inside-out browser because right now if I save it go back to my browser you guys can see we have nothing in here now in order to get out inside out browser what we can do just a test what exactly is going on in here we can do a print our function is a print underscore our parentheses insert my data variable inside of here without the brackets we can actually see the radio we have going on here now right now this can be a bit messy but if you take it from one end and just kind of analyze what we have here you guys can see you have an array which is out data array and inside the array we have number 0 which is the first piece of data and inside our Siro we have another array that has all out data from the database inside of it which is right here now you might be asking why do we put all the result from our database into the first data of our ray well right now when we take results from the database which would do right here when we say to watch while statements and we actually print out all the data it automatically gets seen as an array so when we have an array from the database we get from the database and insert it into another array then we get an array inside an array this is what we call a multi-dimensional array now in the next episode we're going to talk more about multi-dimensional arrays but just for the sake of this exercise we're going to go ahead and just run with it so basically we have an array inside an array now that's important because we want to get this data we need to make sure we go into the first piece of data from our original array and then spit out the result from that array so you guys can see right now we have an idea is equal to 1 we have the next data in the first row which is text that's equal to high and then we have the next day and here we have an ID and another piece of text ok so if you go back to our code we can go ahead and just comment out our print are because we don't need it right now we're going to go down to next line and down here we're going to wouldn't spit out first of all just the first row from our database now the way we do that is the writing a for each statements now for each statements like we talked about before is what we want to loop results from an array next select correctly there we go parentheses curly brackets now inside the parentheses we need two pieces of data we need to have the original array that we have a pickled datas and we need to have the new variable that we need to call on inside the loop so inside the frites parent disease i'm going to go ahead and say we have a variable data which is our array that we have right now and now we're going to go ahead and say as variable data because we want one piece of data from our data array now this data variable here i just created it's not anything that i created before so this is something that we just created inside out for each loop but basically says is that each time we go inside out for each loop and call on data for example here if you want to echo this out then we're going to get the first result from the array so right now if I want to just do this it's not going to work because as I just mentioned we have the first array that has an array inside of it inside the first data so right now it doesn't know what this is we need to actually call on the radio we have inside the first data inside the first array now again multi-dimensional arrays we're going to get it on the next episode but basically we don't want to do about data from the array we have up here want to move out from the data we have inside the first result from our data so we were to go down inside out for each loop and save brackets after data and say zero we're now looking out there ray we have inside the first result from our data array again i'm hoping i don't make this too confusing for you guys so if we refresh your gas conceals has one high which if we go back inside out database is the first result we have from the first row inside our database we have one as an ID and high as the text now this is all good if you want to just loop out one row whatever want all the texts pieces from all the different roles we have been here what we can do then is we can go ahead and delete the bracket we have our after data as and then i'm going to go to go down inside my echo and after the data going to say brackets and say we want text which is a string so need to have single quotes around it and what it basically does is that it goes it looks through this array and sees okay to have any kind of columns from our database called text okay it's going to go ahead and spit out every single data we have inside this column called text so go ahead and save this refresh my browser you guys can see we get high their friend you see seem like a cool person which is what we have inside our database again I'm swimming out here going downwards so this is one way to write it out again I'm just going to go and copy pasted so you guys can see both methods so one is by removing this you can say dat s0 and other one is by running a for each you went is calling on the text column ok so depending if you want to get one row with all the data inside that single row if you want one column inside the database we can do it both ways if i go and refers to braun so you guys can see now gets both of them we get one high and then we start by getting the whole column now I want to have space in between here we can just go to go inside a loop down here and say after our variable data that has two column as text can go ahead and say punctuation double quotes space because now we have a space-going afterwards over to refresh this you can see it says hi there friend you seem like a cool person ok so it's very basic now some of you guys might be asking me why do we need to take the data from the database and put it inside the rate before we use it at least in some examples because essentially we just got inside our database connection up here and if we go ahead and just comment out what we haven't already and underneath inside a while loop right eco-design road brackets and then I call an econ called text and go ahead and refresh my browser you guys can see you get the same result up here now the reason might want to do it the other way is if you're using object oriented PHP programming which is something else where you don't write the code to regular need to use it like we did here right now I need to have all the database results right here so i wrote the code here but an object-oriented programming one of the benefits is that you write the code differently inside a separate document so that the users on the website doesn't see the database connection so for security reasons when you use object-oriented programming i use a race in order to get the data from a database and pass it on to wear need to use it from a separate document so passing the data to the next page is easier for me when i use a race so that's one example of why would actually use it using a race hope you guys enjoyed this episode in the next one we're going to talk about both two-dimensional arrays so you guys can make sense of what we actually did down here and then you guys can go back if you feel like you want to go back and see what exactly was going on in here with this multi-dimensional array be had in here hope you guys enjoyed and I'll see you guys next time