In this class oriented programmers we intend to create our small execution files for any system or web page with php, mysql and PDO. With these files or methods we will be doing all the new programming exercises so that we invite you to stay with us ..
NOTE:
This example of connection and maintenance with PDO, will be the basis to carry out the other exercises that will be published in the future. It is important to learn this example.
What is PDO?
PHP Data Objects PDO is an extension that provides an abstraction layer of data access for PHP 5 , which makes use of the same functions to query and obtain data from different database handlers
How to make a connection to the database and maintenance with PDO?
For this exercise we need to create a folder called corebasic and within it we will create another folder called class . Then we will create two PHP files one will be called conexion.php and the other manto.php .
Connection.php
<? Php
Class Conexion {
Public function connect () {
Return $ connection = new PDO ('mysql: host = server_name; dbname = database_name', 'user_base_data'
'Database_database');
}
}
?>
Continued we will create the file that will have the maintenance for each action, both to insert data to the database, to update data, to eliminate data and to show data.
Manto.php
<? Php
Class Mantle {
#types to insert
Public $ table;
Public $ values;
Public $ columns;
# Attributes to edit
Public $ update;
Public $ set;
# Attributes of showing data;
Public $ select;
Public $ from;
Public $ condition;
Public $ rows;
# Attributes to remove
Public $ deleteFrom;
#metodo insert
Public function insert () {
$ Model = new Connection;
$ Connection = $ model-> connect ();
$ Table = $ this-> table;
$ Values = $ this-> values;
$ Columns = $ this-> columns;
$ Sql = "INSERT INTO $ table ($ columns) VALUES $ values";
$ Query = $ connection-> prepare ($ sql);
If (! $ Query) {
?>
<Script>
Alert ("query error");
</ Script>
<? Php
} Else {
$ Query-> execute ();
}
}
#termine the insert method
#metodo edit
Public function edit () {
$ Model = new Connection;
$ Connection = $ model-> connect ();
$ Update = $ this-> update;
$ Set = $ this-> set;
$ Condition = $ this-> condition;
If ($ condition! = "") {
$ Condition = "WHERE". $ Condition;
}
$ Sql = "UPDATE $ update SET $ set $ condition";
$ Query = $ connection-> prepare ($ sql);
If (! $ Query) {
?>
<Script>
Alert ("query error");
</ Script>
<? Php
} Else {
$ Query-> execute ();
}
}
#termine the edit method
#metodo remove
Public function delete () {
$ Model = new Connection;
$ Connection = $ model-> connect ();
$ DeleteFrom = $ this-> deleteFrom;
$ Condition = $ this-> condition;
If ($ condition! = "") {
$ Condition = "WHERE". $ Condition;
}
$ Sql = "DELETE FROM $ deleteFrom $ condition";
$ Query = $ connection-> prepare ($ sql);
If (! $ Query) {
?>
<Script>
Alert ("query error");
</ Script>
<? Php
} Else {
$ Query-> execute ();
}
}
#termine method delete
#metodo read
Public function read () {
$ Model = new Connection;
$ Connection = $ model-> connect ();
$ Select = $ this-> select;
$ From = $ this-> from;
$ Condition = $ this-> condition;
$ Rows = $ this-> rows;
If ($ condition! = "") {
$ Condition = "WHERE". $ Condition;
}
$ Sql = "SELECT $ select FROM $ from $ condition";
$ Query = $ connection-> prepare ($ sql);
$ Query-> execute ();
While ($ rows = $ query-> fetch ()) {
$ This-> rows [] = $ rows;
}
}
#termine the read method
}
?>
0 comments: