Web app development_crud_13

13
[email protected] July 23, 2013 Hassen poreya Trainer, Cresco Solution Afghanistan Workforce Development Program CRUD Create, Read, Update, Delete

description

 

Transcript of Web app development_crud_13

Page 1: Web app development_crud_13

[email protected] July 23, 2013 Hassen poreya Trainer, Cresco Solution

Afghanistan Workforce Development Program

CRUD Create, Read, Update, Delete

Page 2: Web app development_crud_13

Requirements

Choice of DBMS - MySQL

Database model (ER - Schema)

GUI – Application program

Page 3: Web app development_crud_13

Functions may be used in CRUD

mysql_connect()

mysql_select_db()

include()

mysql_query()

mysql_fetch_array()

mysql_num_rows()

Page 4: Web app development_crud_13

mysql_connect()

mysql_connect()is designed to handle the connection.

You can use this function internally or you can include it from an external source.

This function takes 3 arguments by default.

host, username, password

mysql_connect("localhost","admin",

“admin");

Page 5: Web app development_crud_13

mysql_connect()

To check whether the connection is established or not we store this function into a variable and check it with IF statement.

<?php

$con=mysql_connect("localhost","admin","admin");

if(!$con){

die('Could not connect: ' . mysql_error());

}

?>

You can use this piece of code anywhere in your code anytime, but that’s might be used several times. Then you can put your code in a separate file

Page 6: Web app development_crud_13

mysql_connect() In a Separated File

You code up your connection and db selection into a separate file, then you include that file into the main file. Ex: connect.php contains the connection.

<?php

....

include („connect.php‟);

....

?>

Page 7: Web app development_crud_13

mysqlSelect_db()

Once you have successfully connected to My SQL, then you must chose a database to work on.

Another PHP fucnction is used to select a database from database server.

mysql_select_db(“users”);

Page 8: Web app development_crud_13

Query Conversion

We have SELECT and INSERT queries in Mysql and simply use them during inserting and retrieving. BUT if we want to use them with PHP we must convert them into PHP code.

This conversion is done by a built-in PHP function called mysql_query()

mysql_query ("INSERT INTO users (username,

password) VALUES ('$uname', '$pword')")

Page 9: Web app development_crud_13

Query Conversion

mysql_query("SELECT * FROM users

WHERE username= „ahmad'");

When we use SELECT statement, each time it returns one row of a table.

To store the returned data into a variable we must fetch them one by one and store them.

This can be done by a function called

mysql_fetch_array()

Page 10: Web app development_crud_13

Query Conversion

$query=mysql_query("SELECT * FROM users

WHERE username= „ahmad'");

$result= mysql_fetch_array($query)

Query Fetch row by row from query Record set

Page 11: Web app development_crud_13

mysql_num_rows()

The mysql_num_rows() function returns the number of rows in a record set.

This function returns FALSE on failure.

Page 12: Web app development_crud_13

Let’s do the practice

Page 13: Web app development_crud_13

[email protected] July 23, 2013 Hassen poreya Trainer, Cresco Solution

Any Questions!

Afghanistan Workforce Development Program