PHP PDO Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey welcome to the PTO crash course in this video I'm gonna show you kiddy or how to learn the PD all from scratch in the PHP so let's get started pd o Salafist so first first thing we need to do is they connect to the mysql database as you know the period and connected with all the database so we will test it just within my experience so let's connect to that database so here is my video example it's a PHP file and it's open in the Chrome browser in here so I'm go to the PHP my admin' and here I have a database called test in the test database I have a table called hosts let's connect to this database test so PD PD oh but if you want to connect to the PD oh let's have some certain variables the first thing we need that's their host so what is your host our house is local so the user that our user is root and the password is empty and that database is test so let's create to the let's create the connection today PDO so first thing you need to do is you have to create you have to set that DSN which is the data source name so what is that so let's set that data source name they speak also what is your database here you just say my database is I say my as well so put a column after that and what is your host house view is equal to your house name and then the Terminator this the semicolon and after that you put a DP name it's equal to your database so these are the variables that we said it host name is a variable called host and database is a variable called Dennis so this is our DSN variable that we set in here so the next thing we are going to create the object of the PDO for creating the object of the PD oh you were just gonna say the new PD oh like this so the first thing you need to say is the data source name this variable and the second one is the user and the third one is the password so I store this to a variable called the PDO or something else that you want so I connected to the database so let's check the connection for example I'm gonna use the if statement in here I'm just a fpd hope is connected so here I'm going to say that connection for example successfully something like this else I'm gonna echo error connecting or connection something like this I'm gonna step on let's test it on the browser I'm gonna refresh this page as you can see connections successfully to the database called test so let's clear this text so the next one we connected to a successful Italy which let's see the next one the next one is there run some queries in the test a terrace in the this is my test database and it has a table called posts let's see the structure of this table so it has an ID which is primary key and it is an auto increment as well the next one it says title virtual hundred body type text and other and it's a warder 100 so let's browse this table this is the data in the table that I've inserted from here so let's run some curious to access this data from the PHP with PDF so you know I'm going to say run some course and here I just gonna say that PT o here from the object of the PD oh you just go you just said it query in the query will you write your SQL query so I'm gonna say a select all from day yeah table and the table is posts so this query will exact you so here the PD Oh what you're gonna do no no no sorry please I'm going to store it it's a statement for example a statement what executor exactly so let's fridge the data from here so I'm gonna say fetch when I set a keyword rich it will just fetch the first row in the table so I'm going to store it in the variable called for example fetched so here I just want to do something for example let me save this and refresh the browser as you can see we have no error and some right now so I'm gonna say that print are and let's see that fetched as you can see it's an array with an ID it's a 1 it's a title this is the title and this the post body and many more so it's just the first row if you you fetch that all of these rows you just say the fetch all so here let's refresh this as you can see we have two rows in here and we when we set the fridge all it will select all the rows on the table and it's [Music] so let's echo this for example in a table I'm gonna create a table in here in the HTML so this is our zero this is our heading for example they see the database ID title body and button it's ID and then it's a title it's for example body and the next one is dead yeah autumn so this is for further for our first row so I here I'm going to say that border is equal to 1 and the next row is it have to be dynamic so this is the title I'm gonna say just PHP open the PHP and here I echoed a fetched variable from fixed worried about what we are gonna do here you just said for example first row second row when you want violet I say just for example dead from first row just a Conformity I did so whenever I see this on the browser when it says AHA right i D as you can see this is the idea that we get so let's get there yeah I copy and paste this and here this is for the title oh all right quickly this is the title and let's get the body and let's get the author this this is the body and this is the audio so i refresh this and this is as you can see this is just first row if I wanna the equal with the second row let's get to show lo easier so you know I'm gonna say just a video for each row for which for example fetched as for example room so I open the discordant process in here and I close this in here I want to fetch this so I want to refresh this as you can see this one group repeated two times what does it mean because it is not dynamic is this static so what we are going to do is we just say that row in here get from radhe ID tidal body and auto I service and i refresh as you can see this is now dynamic so let's get all the data in here now I have here this is the post idle toward is the post I love one so this the bottle charm Rose so and this is highway run query and Nitz and this is the we fetched so the next one is free square root that we fetched in here's by fetch so you can fetch this by with two kinds for example to table you can see you can fetch first one is the fetch by a sock for example this the default in the PHP if we do fetch a so this is the default that we didn't set it and you can tell that the fridge paid off edge by what by object so here whether you say that PDO fetch a sock refresh the page nothing's happened because it is that default fetch in the PHP PDO so let's change this page for example I want to change this page by object I save it and I run as you can see this is Aaron so let's fix this it is you get the data by word by fetching by the associative so let's get from there getting data by fetching the object we fetched by the object in here so here you're just gonna say that Eco row here like this you treat this row like an object robot row ID so this is two different I'm gonna delete this so here just gonna need you just say that like this row are to get our dough this is how you can fetch the data by object so I'm gonna refresh this as you can see this order post right now that are fetched by object so these are this is how you can fetch and buy a soak and object so set attribute set attribute to what suppose that I want to write another query write another query in it's me for example this is another query I will write I just say that this is the query for example this is the post and I'm gonna stay here day ID is equal to something like 1 and this is like this and here I'm gonna finish this for example by a soak and I don't want to do this for example by which a soak and object in all when while I'm fetching so here for doing that I'm just gonna say that here I just said that attribute a default fetching mode so that is the PDO set a tribute here I'm gonna say the pitiful attr means attribute default French mode and the next one is then peel it off and fetch why for example object it is by default it's the object I don't need to hear tell this day object but you can overwrite this role as well for example I set this attribute like this and I want to I want here to fetch by a so here you just need to say PDO fetch a so this will be f hu by day also so this is the set attribute to fetching by default so the next one is the property statement prepare and execute whenever you want to run your queries you want to build your website if you run queries like this PDO query and your career is like this it is not secured it's unsafe it's not safe so whenever you want to you want it to be safe so what you need to do you need to learn about your prepared statement so let's run some queries for example it's I want to run our query this query but securely by property statement here I'm just gonna say that P D o P do what beautiful purple and here you write your query like this and it is not safe it's not safe as a real for example where I'm gonna select a specific ID a specific row for example it's an ID right into ID in queue it's an idea like this I so statement statement exactly this is executive an executive this query so now you can fetch statement fetch oh this is the one room I'm gonna say just fetch so it is fetched I'm gonna delete this this is the ID which comes from the user input for example and this is the PDO proper select all from the posts we are by vehicle to a bilingual identical to one it's gonna select that first row so I'm gonna save this and I'm going to refresh this page in here what is is ID yes right so prepare its electron from posts we are in Kentucky statement exec you and first that will get by the archer power yes right here clear just look but you just need to do you just need to say this fetched first first get that funny face to get the auto refresh this as you can see this is the title so this is not a safe way to fetch the queries like this so I'm gonna say of securities as well to securing this the first thing you need to do is positional parents you can save your current by positional partners by getting parameters by their position for example Q I'm gonna say I'm not gonna do their ID on you I just put the question mark so this is Devon I don't like get exactly and in exact you in here I put a curly square bracket like this and I write their ID in here this is point two here for example in whenever I want to save your ID equal to this question mark and so let's run this query first I'm gonna save this and i refresh the page we get the same result so whenever for example I want to have multiple we are I equal to question mark and I don't equal to a question mark and then in here just put the comma type the title in here I'm not I just literally so the title for example title i refresh the page as you can see there so I think nothing's selected so post a title one I'm gonna copy this from here let's write in here post a title one as you can see this one record is selected so this is a secure way for doing that by positional parameters and the next one is the named parameters name the parameters is a little bit different so what you just need to do is you just need to do something like here I'm gonna say named harms this is the ID select all from the post we are adding not question 1 here I'm just saying ID title not question mark here I just say title so executive ID here not you just say that I think here you point to decide like this you type the ID equal to this body and then the title equal to this title so this is how you can do them so I'm just going to comment this refresh in here as you can see we get the same reason so this is the named parameter so let's see the next one the next one is get a row count for example I'm gonna do that they use this input as like this for example and nothing's selected and what the errors are displayed so here when you're going to you just say if our room if a row is more than zero row it means that something selected so if there's not don't shoot an arrow so for that you're just gonna say that a statement row count this method and at the store let's store it into in a variable found Rose found Rose I'm gonna say that F found Row is I open the PHP tag in here I'm gonna say F found Rho is equal to 0 so do something in here tell nothing's selected or nothing's found else else means something's found I'm gonna open this me here and I'm going to close this in here like this i refresh the page it's it tells me nothing's found and it don't show me that error so whenever I write it like one row selected in here this destroys an exists ID is one and the post item is post title one so I'm going to refresh the page and as you can see one row is selected and it shows me on the table so the next thing this is the rocker and next thing the next one is the insert death on this in circles and data in the table so I'm just gonna delete this comment insert data here I'm just gonna say this is that I write the query in here insert into the posts and the ones to which columns you wanna insert by this watch increment I'm gonna I'm gonna insert in the title body order title body and the values are I'm gonna use in the real world project is a logic we use is named parameters so this is the title I say this is the body and this is the auto so let's prepare for this query pretty Oh proper and you put a query in here so it's a what it's a statement that will insert and let's execute this statement executes so let's put those values in here I'm go right to here like this for example the title is the title is for example title and body is the body and the art of is the part so let's put a user input guitar in here for example this is our title this is our body this is our Archon others for example titles for example post-test title for example test item body is test body and the author is test other so whenever it gets executive and in the last I'm gonna say that echo echo what record add it successful I'm gonna save this I will refresh this this page as you can see record added a successful so let's see in the there is a hero posted LT takes his body and test author is added successfully in the devil's so this is how you can insert your data in that database so the next one is they get the last five you this is a very useful technique this is a very useful method to get the last ID for example I'm a user on your website and I go to the sign up page I sign up myself on your website and you want to redirect me from the signup page to home page and you want my ID and the one dear you just say when you insert the data on the database you just call this to get you just call this method and that is the last ID method and by this you can access to my ID so let's test that let me comment it out like this so get last ID so no no I don't comment it here I just say that a beer in here and I say and the last ID is and here I'm gonna say that the PD Oh insert last insert ID last insert fighting this is this is the middle so let's put the test at title for example - body - Otto - this is this would be the next row I'm gonna as you can see record added successfully and the RIT is and the last idea is for as you can see this is the idea for like this you can in the real world project you can put it in a variable and you can redirect today another page that can be home page or something else like that so this is the gate last I - the next one is the insert multiple data whenever you want to insert some multiple data what you need to do you need to do something like again statement executes and you write your queries in here like that and this is for day for example is a four day first title first title first body and the first part this is for the first rows first years our input for second this is for this second for example right now so this is body one or two one this is the - you can add many many rows so here before exact healing what you need to do is you just need to do to start this bacon front section begin transaction and at the end here you need to commit the transaction feel like this records added successfully I don't get this so I save it i refresh the page as you can see records added successfully two new repairs should be added here as you can see this true new record added in one time refresh so the next one is the update data how you can have that data update data here you write your query in here so Kerry just say update which table posts you set the title for example equal to what equal to title I use the named parameters so the next one is the body equal to what equal to body and the last one is there are two is what other so we're ID is equal to what it is ID so let's get proper to this statement pretty Oh prepare this query and stored in the statement variable and this is statement variable get exactly and you're right the values here so the title is the title is the title and you just say something like this for example here yes the title bodyguard oh so this is and the last one is the ID sorry this is the ID and this is the idea that you want to update so that is the idea is for example I want to update the ideal one with a data of this the second title second body what will I do what will update this this postman updating here so I'm gonna before that let's say that that echo echo out the record updated successfully so array chord updated successfully so here I just refresh this record updated successfully the first I think that first row should be updated as you can see second title is second part is you can either Jesus updated so this is the update and the last and this one is dead delayed data how we can delete data from the database so it's deleted open the database delete data so writing your query in here and that is delayed from what from hosts Real ID equal to ID so let's get prepared for this purple for this query and store it in a variable called statement and this could be any variable that you want so I yeah a lot used a statement variable so it is the statement worried about get what get the executive but data the ID the ID in here which I do you want to what didn't for example I want which item for example this plate is this ideal sex so it is equal to six delay from poster artists equal to six the sexual could in here so after getting executive I just record the record deleted successfully so I'm Grover fresh this page they record the latest successfully so here i refresh the browser as you can see that ID sticks is not in here that is deleted so the next one is nothing don't forget to practice thank you very much and see you in the next video
Info
Channel: Semicolon
Views: 696
Rating: undefined out of 5
Keywords: PHP, Coding, Programming, Learning Programming, Learning PHP, PHP PDO, Crud with PDO
Id: fqvKbuqNgjM
Channel Id: undefined
Length: 36min 36sec (2196 seconds)
Published: Mon Mar 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.