Andorid PHP mySql
Creating Database
MYSQL database can be created easily using this simple script. The CREATE DATABASE statement creates the database.
<?php $con=mysqli_connect("example.com","username","password"); $sql="CREATE DATABASE my_db"; if (mysqli_query($con,$sql)) { echo "Database my_db created successfully"; } ?>
Creating Tables
Once database is created, its time to create some tables in the database. The CREATE TABLE statement creates the database.
<?php $con=mysqli_connect("example.com","username","password","my_db"); $sql="CREATE TABLE table1(Username CHAR(30),Password CHAR(30),Role CHAR(30))"; if (mysqli_query($con,$sql)) { echo "Table have been created successfully"; } ?>
Inserting Values in tables
When the database and tables are created. Now its time to insert some data into the tables. The Insert Into statement creates the database.
<?php $con=mysqli_connect("example.com","username","password","my_db"); $sql="INSERT INTO table1 (FirstName, LastName, Age) VALUES ('admin', 'admin','adminstrator')"; if (mysqli_query($con,$sql)) { echo "Values have been inserted successfully"; } ?>
PHP - GET and POST methods
PHP is also used to fetch the record from the mysql database once it is created. In order to fetch record some information must be passed to PHP page regarding what record to be fetched.
The first method to pass information is through GET method in which $_GET command is used. The variables are passed in the url and the record is fetched. Its syntax is given below −
<?php $con=mysqli_connect("example.com","username","password","database name"); if (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $username = $_GET['username']; $password = $_GET['password']; $result = mysqli_query($con,"SELECT Role FROM table1 where Username='$username' and Password='$password'"); $row = mysqli_fetch_array($result); $data = $row[0]; if($data){ echo $data; } mysqli_close($con); ?>
The second method is to use POST method. The only change in the above script is to replace $_GET with $_POST. In Post method, the variables are not passed through URL.
Android - Connecting MYSQL
Connecting Via Get Method
There are two ways to connect to MYSQL via PHP page. The first one is called Get method. We will use HttpGet and HttpClient class to connect. Their syntax is given below −
URL url = new URL(link); HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(); request.setURI(new URI(link));
After that you need to call execute method of HttpClient class and receive it in a HttpResponse object. After that you need to open streams to receive the data.
HttpResponse response = client.execute(request); BufferedReader in = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
Connecting Via Post Method
In the Post method, the URLEncoder,URLConnection class will be used. The urlencoder will encode the information of the passing variables. It's syntax is given below −
URL url = new URL(link); String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8"); data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8"); URLConnection conn = url.openConnection();
The last thing you need to do is to write this data to the link. After writing, you need to open stream to receive the responded data.
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); wr.write( data ); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
Comments
Post a Comment