Android MySQL Database Tutorial 1 - Creating Database And Writing PHP Script

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome to the first video on Android MySQL database tutorial for beginners in this video series we are going to learn how we can create an Android app using MySQL database which is running on some web server so let's see what are the requirement first of all we are going to need a MySQL database and then we want to use a web server web server can be an Apache web server or nginx web server or any other web server we can also use a web server which is running on web hosting right now we are also going to need a server-side scripting language in our case we are going to use PHP for server-side scripting so this server-side scripting language will help us to query the database and cater the result and the last we are going to need our Android development environment which we are already using which is Android studio now there are different ways of getting these databases and web server and scripting language together so for testing purposes what we can do is we can download and install a ramp server and what is a web server web server is a Windows web development environment and it allows you to create a web application with Apache PHP and MySQL database alongside it's also going to give you PHP my and win which allows you to manage your database easily ok so this is the first option which we can use for creating our first Android application for and for testing our application now for practical uses we can use web hosting and a server which is running on some web hosting website like GoDaddy or who skater or Google hosting or any other hosting website okay so what is a web hosting web hosting is a service which you can use to you know host your website and it generally comes with you know file system and database integrated so we can use this web hosting also so I'm going to give you both examples using ramp web server also and this web hosting also so you can create online you know MySQL database using Android application right so first of all we are going to create an application using ramp server it comes with MySQL Apache and PHP myadmin so let's download and install vamp web server and then we are going to write some PHP scripts to query our database and then we are going to create our Android application so first of all open your favorite browser and search for web server and the first result most probably which will appear here will be from web server comm slash en for English just click this link here and this is the website from which you can install vamp web server so you can read more about web server in here and how you can install it so to download web server executable file you just need to click download section here and in here you have two options one is web server for 32-bit operating system and second is the web server for 64-bit operating system I have 64-bit operating system so I'm going to click this option right now in here you can read the prerequisite for installing the web server so you can read here don't use previous web server extension or add-ons so you must not have any other or previous version of web server when you are installing it right the second warning here it says that this web server requires Visual Studio 2012 we see eleven executable file so to install this web server you need to have Visual Studio 2012 we see 11 and to download and install it you can just click this link so first of all I'm going to click this link to install this executable and then we are going to install web server so just click this Microsoft website link and in here you can see we are going to install visual c++ redistributable for visual studio 2012 update 4 ok select your language and click download and in here we are going to check this first option which is for 64-bit operating system if you have 32-bit operating system you can choose the second option or if you have arm device you can choose the third option so in our case we are going to choose the first option then click Next and now the executable will be downloaded you can see our executable is downloaded so just click this executable file here and then click run and then just check this checkbox to agree with license terms and condition and click install and click yes and once Visual C++ distributable installation is complete you can just close it and once again you can open your favorite web browser and then search for ramp server and then go to the same website web server dot-com and then once it go to downloads here and then once again click the web server option and then this time you need to just click download directly ok so it's going to download this web server executable right so we will wait for this web server executable to download it's not a big file it's just 41 point 5 MB file so it's going to download in few seconds now once the download is complete you can click this executable file for the web server and you will be able to see this kind of web server setup so just click Next and in here just click I accept the agreement and click Next and this is the location where web server will be installed so C colon slash vamp will be the folder in which this web server will be installed so just click Next and then you can create a desktop icon and quick launch icon also so I'm going to create only desktop icon for that and click Next and click install now in here it's going to tell you or ask you please choose your default web browser and if you're not sure just click open or if you have some browser preferences you can choose your browser from here for example I want to choose a Chrome web browser so I'm going to go to my C folder and in here I'm going to go to Program Files x86 and in here go to Google folder and go to Chrome application and choose Chrome dot exe and click open ok and then it's going to finish installing and you can see the web server icon here ok and right now you just need to allow the access you just click allow access here and these are the PHP mail parameter which we are not going to use right now so we will leave them as default and click Next and then just click finish and please make sure that this option is checked to launch the web server right so just click finish and click yes and it's going to launch your web server and when your web server is ready it's going to show you this green ramp server icon here right so you can just click this web server icon here and you can see all these options here ok so right now you can see PHP myadmin apache mysql folder and other options for web server so first of all what we are going to do is we are going to use PHP myadmin to create our database so let's create a database now you can see our PHP myadmin is open here and by default it has some databases right so to create a database what we can do here is you can click data yes and give the database name so for example we are creating a database for employees so we can just write employee 101 and then just click create here and you can see our database is created so expand to your database right now we don't have any table inside the database so just click this employee database and in create table option you can just create the table inside your database so for example we are going to create a table called employee data and then we are going to provide the number of column it's going to contain for example it's going to contain employee ID name surname age and user name and password so it's going to contain six columns just select 6 from here and then click go and then it's going to give you this kind of wizard first is our ID second is the name third is the surname fourth column is the age fifth column is the user name and sixth column is the password okay now ID is integer name is text surname data type is text also age data type is int itself user name data type is text and the password data type is text okay now we can also choose the primary key of this table so ID is our primary key so go to index here and then choose primary for ID okay and this ID is also going to be Auto incremented so choose this option which says a underscore I which is auto increment okay and then once this is done we can just save our table okay so now our table contains six columns which you can just click on this employee table then you can see all the columns inside the table which is ID name surname age and username and password columns right so our database and database table is ready to use now after creating our database we need to write some PHP script to connect to the database and then fetch the data or inserts the data using this PHP script so now what we need to do is once again click this web server icon here and this time click this WWWE option here ok so just click this option dub-dub-dub directory which is going to open this vamp server / dub-dub-dub directory and in here you can see by default index dot PHP and test MySQL dot PHP here ok so what we can do here is we can create one more file you can just create a new file from here it can be a PHP file ok or you can open a notepad plus plus and in here you can create a PHP file so just open a new tab and then save it as a PHP file so first is the connection of PHP file ok so I'm going to just choose the same folder from C so C vamp and then in the vamp we are going to go to www-wait a PHP file which is connection PHP so I am just going to right corner dot PHP ok and then click Save and it's going to create this file which is con dot PHP here ok so now we can write some PHP script to connect to the database so how we can write it first of all you just need to write this PHP initialization element and between this PHP element we are going to define some variables and to define a variable in the PHP you need a dollar symbol and then the variable name so for example first is our database name and which is going to be equal to we are going to define these names in a while just give a double quotation symbols right now okay second is the MySQL username so just write my SQL underscore username okay is equal to leave it blank for now and then third option is the mysql password okay so just write MySQL for example password okay and the fourth option is the server name right so the fourth option is the server underscore name okay so now first option is database name so database which we have created is this database which is employee 101 right so just copy the name of the database and go to your PHP file and give the name of your database right the second option is the MySQL user name so we can see the MySQL username by going to the database so just select the database and then see the privileges and in here you can see the username is root and the privilege is all right so for the username we are going to choose from here and we are going to just paste here because our username ends root and the password what is the password so for example we are going to use this localhost as a server name so we are going to just write localhost and in here we can just go to edit privileges and go down a little and right now there is no password or you can just select this option which is no password and then click go and it's going to you know save your changes successfully so password is nothing or blank okay now after that to connect to a database we are going to create a variable called corn and we are going to initiate it with a method called MySQL I connect so just write my SQL i underscore connect and then inside the brackets just pass the four arguments it takes four argument first is your server name so just pass the first argument as server name second is the MySQL user names second is this one third is MySQL password so just copy this variable and paste it here and the fourth is the database name so database name is this one okay so these are the four arguments right now to check if the connection is successful or not you can just give this condition if inside the if statement you can just write this variable corn which gives you the boolean result and then inside these brackets if the connection is successful you can echo something so you can just write echo connection success otherwise in the else condition just write else here and then in the else condition you can add Co connection not success okay so just write echo connection not success okay and then save everything and then open your web browser and in here you just need to first of all right localhost which is going to give you this kind of web server options like this right and then whenever you give localhost / corner dot PHP which is of a file name if you remember inside our www folder we have this corner dot PHP file so in your browser you just need to write localhost and then slash corn dot PHP right and when you click enter it's going to give you connection success if your connection is successful otherwise is going to give you connection not success so let's add it our you know these values so we can know that it gives the right result so I will change the database name to wrong the database name and save it and once again refresh this localhost / con dot PHP and now it's going to give you this error report right and it says connection not success so we know that whenever we write this write parameters it's going to give us the connection success right so right parameter gives the connection success okay so in this way we know that our PHP is able to connect to the MySQL database right now we are going to create one more PHP script so I'm going to just write create a new PHP script and I'm going to save it as for example login dot PHP so just write login dot PHP and click Save here and once again I'm going to just give the PHP initialization tag and in here first of all we are going to you know import the score dot PHP which gives us the connection right so we are going to require first of all so just write require and then inside the double code corn dot PHP corn dot PHP right so this is going to import this connection and then we are going to create two variables one is user underscore name which we are going to give in a while and right now we are going to leave it as blank and second variable name is the user password so just write user pass and leave it as blank for now because we are going to you know enter some data inside the table and then we are going to check against it okay and then we are going to prepare a query so just write my SQL for example query okay ready for qry and in here we want to check if the username and password is correct so we can just write select some element or you can just write select asterisk which is a wild-card for selecting everything from your database name so our database table name is employee data if you remember this one is our database table name so just copy the same name and paste it here and then just write where clause and in here we are going to check username and password right so we want to check the column name username and password right so where user name like your username variable so in the single quotes you just need to copy this variable and paste it here and then just give a space here and in here after that just right end and then take your password column name which is this one and paste it here and then once again single quote and inside the single quote you just need to give your user pass variable name which is this one right so I'm going to just minimize this a little and just paste it here okay and don't forget to give this semicolon before this double quote and after this double quote also now we are going to execute this query so we want to create a variable called for example result is equal to and then we need to call a method called my SQL underscore query so just write my SQL query and as an argument we need to you know parameters one is the connection variable which we have here in connection dot PHP which is this one and the second argument is our query which is this one right and then we can evaluate our result using a if condition so just write if our query provide more than one line of result then we know that username and password is correct so just write my s ql i underscore num underscore rows and then inside these bracket just pass this result variable so just pass result and we will evaluate if it's greater than zero then we will say that our user name and password is correct right so in the echo we can just echo logging success right so just right login success okay and otherwise if there is no result we are going to echo that login is not successful so just copy the same and just say login not success okay and save your code now in your database you don't have any data so these two variables we have left right now blank but let's insert some data inside our database so just you know select your employee data table and select this option which says insert here so just select this and in here we are going to insert some data so for example employee ID one name is Tom and the surname is for example something and the age is 22 and and username is one two three and password is one two three okay and just click go it's going to insert this username and password so go to browse once again and now you can see this data is added to your table employee ID one Tom ban age is 22 and username and password is one two three and one two three so when we write here 1 2 3 1 2 3 and password is also 1 2 3 and save it and in here I think this should be MySQL I query so instead of MySQL query you need to write MySQL I underscore query right and save your PHP file go to your web browser and just write localhost and then this time you just need to lie it login dot PHP and now it's going to give you connection success and login success that means your login is success using the PHP script so right now we are done installing our vamp webserver and we are done writing our PHP scripts so in the next video what we are going to do is we are going to create our Android app and we are going to connect it to our MySQL database using this PHP script so stay tuned and please read come and subscribe and bye for now
Info
Channel: ProgrammingKnowledge
Views: 483,249
Rating: undefined out of 5
Keywords: Android, programming, tutorial, beginners, develop, mobile phone, Environment Setup, Application Components, Activity Lifecycle, Service Lifecycle, Application Architecture, Publishing Application, Debugging Applications, Handling Events, Layouts, User Interface Controls, Styles and Themes, MySQL, Database, PHP
Id: HK515-8-Q_w
Channel Id: undefined
Length: 28min 46sec (1726 seconds)
Published: Sun Jan 03 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.