Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Connection {
- private $connection;
- private $host='localhost';
- private $user='user';
- private $pass='pass';
- private $database='dbname';
- private $n=0;
- function __construct() {
- $this->connection = mysqli_connect($this->host, $this->user, $this->pass, $this->database);
- mysqli_query($this->connection,"SET NAMES 'utf8'");
- }
- function sql($sql) {
- if (is_array($sql)) {
- foreach ($sql as $s) {
- $res = mysql_query($s);
- if (mysql_errno()) echo mysql_error().'<br>';
- $this->n++;
- }
- } else {
- $res = mysqli_query($this->connection,$sql);
- if (mysql_errno()) echo mysql_error().'<br>';
- $this->n++;
- }
- return $res;
- }
- function __destruct() {
- if (is_resource($this->connection) )
- mysql_close($this->connection);
- }
- function getLink() {
- return $this->connection;
- }
- }
- So if I want to Create, Read, Update, Delete Something I do this:
- In another file: User.php (Which i want to use the new method I'm asking for and use prepared statements for the Queries here and escape functions)
- <?php
- // Declare a variable
- private $user;
- // Setter
- function setUser($val){
- $this->user = $val;
- }
- // Create Function to CreateUser
- function CreateUser(){
- $con = new Connection();
- $sql = "INSERT INTO user (DATA) VALUES(DATA)";
- $con->sql($sql);
- }
- // Same for all CRUD i just change the Query of course.
- ?>
- <?php
- // At the index.php i do this:
- include_once 'User.php';
- $user = new User();
- if(isset($_POST['createUserButton'])){
- // $The variable i set in my User Class
- $user = $_POST['user'];
- // Then the Setter
- $user->setUser($user);
- // Then execute the Function
- $user->CreateUser();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement