37: How to show database data on a website using MySQLi | PHP tutorial | Learn PHP programming

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the last episode we talked about how to actually connect to a database and in this episode we're going to learn how to taxi to let data from inside the database you connected to so as you guys can see I started off from the previous episode where we just simply included a database connection inside our DBAs the link to PHP file and we're going to use this connection in order to actually go into the database and select data and then spit it out inside our website so inside my index file I included my database connection at the top here so we accept access to variable con and inside the index file inside the body tag I'm going to go ahead and include a piece of code that goes into the database selects a piece of data and spits it out so before we get started I'd like to show guys what exactly I did inside my database so inside PHP myadmin I created a database called login system because that's what I connected to inside my DBAs file call login system and inside this database I went ahead and created a table called users which we're going to use when we create the login system a couple episodes from now and inside this users table I haven't actually included any kind of users yet so we can actually go and do that together so inside the description of this video inside YouTube you guys can find a link to my lesson files for this lesson and inside those lessons files I will have a document called database code that SQL and inside this file you can actually find the SQL code that I used note to actually create the table core users and the insert statement that we're going to use notes actually insert a user inside the users table so I'm going to copy the code from the bottom here called insert into users plaplapla that will actually insert a user inside this table up here so I'm going to go inside my login system database I'm going to SQL at the top here I'm going to paste in the code and I'm just going to go and include another user so we have two of them inside the database I'm just going to change the information of the second one so it's not Daniel instead we can say Jane Doe and we can change the the actual email to just gain at gmail.com we're going to change the username from admin to gain two four five a of something we're going to change the password to test one two three four just to have something I'm going to go ahead and run this code and as you guys can see now that'll go inside my users table we now have two users inside this table here okay so one thing to bear in mind that I want to point out is that inside my table the first row or the first column inside the table is called user ID now the use ID when I created the table if you go back to the code is set to auto increments and because it's Auto incremented we don't actually need to insert any kind of data inside this column here so that's why we're not actually inserting into a user underscore ID okay so now we have to use this inside the database I can actually go back inside my website that has nothing inside of it right now and we can actually go ahead and write the code that will select the data we just insert it inside the database so inside my body tags here since we already have the connection up here I'm going to write the PHP tags so we can actually write PHP code and inside of here the first thing I'm going to do is I'm going to select the actual data using a SQL statement so I'm going to go ahead and say we have a variable called SQL I'm going to set it equal to double quotes semicolon now inside the double quotes I'm going to write one of the SQL statements we learn them on the previous episodes called select so you guys should not have to actually do this by now so I'm going to right select then I'm going to select everything from users because that's the name of the table then I'm going to go ahead and say well let's actually just go ahead and do this then I'm going to say semicolon now you might be asking why do I include a semicolon we have a semicolon because some people have been writing me about that now when it comes to using SQL code inside PHP code right now we should only look at this code as what is inside the double quotes so right now if I were to actually write code inside my database for example like we did here with the insert statement you guys will notice that there's a semicolon at the end of the SQL code so that's something that we need to do inside SQL and that's why inside the SQL statement inside the PHP code I also include a semicolon at the end here so this semicolon outside is the PSP semicolon and the semicolon the inside is the SQL semicolon okay now that we have the actual SQL statement want to insert inside the database that lets you do something and return something to us we need to actually query this code and it's actually called querying the code we want to send it to the database and run it inside the database so the way you do this is by creating another variable we're going to call this one results I'm going to set it equal to a PHP function called my sqli underscore query parentheses semicolon and then we need to go inside the parentheses and the first parameter in here there's going to be two the first one is going to be actually connection to the database which we included inside DBAs dot Inc the PSP which is right here variable con so I'm going to copy this variable which we have access to because a link to it up here and insert as the first parameter then the second parameter is going to be the actual SQL statement we'll want to actually query inside the database which is the one we just wrote here like so and now I'll actually go in and fix the results but now we need to actually take the data that we got from the database and be able to actually spit it out inside our website so the way we do that is well first of all we could actually take if you had any kind of results so I'm going to create a variable called result check if you wanted to this is optional I recommend you do it though because if you don't get any kind of results from the database you're going to get a error message inside your website so it's a good idea to tick with this sort of thing so we're going to say result check is equal to my SQL I on the score num on the score rose parentheses semi-colon and then I'm going to insert the query that we just ran inside the database and put it inside the parentheses so it just takes one parameter so now that we have the actual result number we can actually go ahead and check if we have a result above zero so in the next line I'm going to create an if statement that says if we have results check and it's actually greater than zero then do whatever is inside the curly brackets so right now if it did actually have any kind of result from the database when we want to select something then it's going to run this code inside the curly brackets so we've got no results it's not going to do anything here okay so inside the if statement we can actually go ahead and say okay now we got some kind of result so now what I'm going to do here is I'm going to go ahead and spit out the data now for were to create an if statement that says if we have data then spit it out then we're only going to spit out the first data from inside the database now because it selected everything from the user's table I would like to keep spitting out data as long as we have results so what I'm going to do here is I'm going to go and change the if statement to a while loop that will actually keep spitting out data as long as I have data inside the database now inside the while loop condition I'm going to include a variable called row which is going to be set equal to a PHP function called my sqli underscore fetch on the score as suc which is going to fetch all the results from the result we have up here so I'm going to insert the variable result inside the parentheses and it's going to assign an equal to dollar sign row so basically what's going on is we get all the data from the database and we insert each row of data inside those on row as an array so because those are row becomes a array with all the data from the database the way we included or the way we echo it out inside the website it's the same way as an array so if I were to go inside the while loop II and actually say I want to spit out for example to use the name from the users I'm going to say echo space dollar sign row and then the way we would actually take out data from an array is by using brackets and inside the brackets I'm going to say single quotes usually inside an array we could actually say 0 or 1 or 2 or 3 because we want to get out the first or the second or the third data inside the array but because when we assign the data inside those on row each data result will actually get a name assigned to it which is going to be equal to the columns inside the database mean there right now if you want to fetch the data from inside dollar sign row and you try to write the name of the column we want to access which is a really neat feature if you want to access data result easily without getting confused about which data wax is spitting out so inside the brackets say if I want to get the username I just simply write user underscore UID which is the column name for the actual username and then I can go to the website and actually see what happens here so now you guys can see we get admin and we get gain 2 4 5 8 which is the user names of the actual users I could actually go and include a break right next to the code here just so we can see exactly what's going on so we're going to say break like so and then we connect to see the names easier so this is how we can actually access data from inside the database using PHP code of course as other variations of how we can actually select data inside the SQL statement for example right now we're just liking everything from inside users I could also say want to take everything from users where maybe user underscore first which is the first name of the user is equal to single quotes Daniel or something then we go back inside our website refresh you guys can see we only get admin because I'm only selecting the user that has Daniel at the first name so we can also do a bunch of variations to this SQL statement up here just so you don't think can only select every from users or something so hope you guys enjoyed in the next episode we'll talk about how to actually insert data inside the database hope you guys enjoyed I'll see you guys next time
Info
Channel: Dani Krossing
Views: 496,809
Rating: undefined out of 5
Keywords: How to select data from a database, get data from database, select data from database, show database data on website, display database data on website, how to get data from a database using php, php, php tutorial, php show data from database, how to display data on a website using php, how to show data on a website using php, how to show database data on a website using php, how do you show database data on a website using php, how to show database data on a website using mysqli
Id: 0YLJ0uO6n8I
Channel Id: undefined
Length: 11min 19sec (679 seconds)
Published: Sun Jul 16 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.