Escolar Documentos
Profissional Documentos
Cultura Documentos
By Email:
Custom Search Search
Enter email
Subscribe
Update: first I had published the db.class.php file implementing MySQL which is deprecated. So I still
Most Popular Posts leave the first db.class.php/mysql version and I give you the db.class.php/PDO version (the one I
recommand).
Drag and Drop Multiple Files Upload
with HTML5, jQuery & FormData
(/html/drag-and-drop-multiple-files-
upload-with-html5-jquery-formdata)
PDO version
Very precise jQuery/Ajax Star Rating
Plugin Tutorial (/jquery/very-precise-
jquery-ajax-star-rating-plugin-tutorial)
function __destruct() {
$this->disconnect();
}
25.
function connect() {
if (!$this->conn) {
try {
$this->conn = new PDO('mysql:host='.$this->host.';dbname='.$this->baseName.'',
30. }
catch (Exception $e) {
die('Erreur : ' . $e->getMessage());
}
35. if (!$this->conn) {
$this->status_fatal = true;
echo 'Connection BDD failed';
die();
}
40. else {
$this->status_fatal = false;
}
}
function disconnect() {
if ($this->conn) {
50. $this->conn = null;
}
}
function getOne($query) {
55. $result = $this->conn->prepare($query);
$ret = $result->execute();
if (!$ret) {
echo 'PDO::errorInfo():';
echo '<br />';
60. echo 'error SQL: '.$query;
die();
}
$result->setFetchMode(PDO::FETCH_ASSOC);
$reponse = $result->fetch();
65.
return $reponse;
}
function getAll($query) {
70. $result = $this->conn->prepare($query);
$ret = $result->execute();
if (!$ret) {
echo 'PDO::errorInfo():';
echo '<br />';
75. echo 'error SQL: '.$query;
die();
}
$result->setFetchMode(PDO::FETCH_ASSOC);
$reponse = $result->fetchAll();
80.
return $reponse;
Top
}
function execute($query) {
85. if (!$response = $this->conn->exec($query)) {
echo 'PDO::errorInfo():';
echo '<br />';
echo 'error SQL: '.$query;
die();
90. }
return $response;
}
}
MySQL version
(/log-in)
<?php
class db {
Home (http://www.tipocode.com/) private
(http://www.tipocode.com/) Link Directory
$conn; (/link-directory) jQuery (/jquery) PHP (/php)
HTML (/html) CSS (/css) private $host;
Database (/database) Contact (/page/contact)
5. private $user;
private $password;
private $baseName;
private $port;
private $Debug;
10.
function __construct($params=array()) { Top
$this->conn = false;
$this->host = 'localhost'; //hostname
$this->user = 'user'; //username
15. $this->password = 'pass'; //password
$this->baseName = 'dbname'; //name of your database
$this->port = '3306';
$this->debug = true;
$this->connect();
20. }
function __destruct() {
$this->disconnect();
}
25.
function connect() {
if (!$this->conn) {
$this->conn = mysql_connect($this->host, $this->user, $this->password);
mysql_select_db($this->baseName, $this->conn);
30. mysql_set_charset('utf8',$this->conn);
if (!$this->conn) {
$this->status_fatal = true;
echo 'Connection BDD failed';
35. die();
}
else {
$this->status_fatal = false;
}
40. }
return $this->conn;
}
$return = $tmp;
70. }
@mysql_free_result($cur);
return $return;
}
75.
function getAll($query) { // getAll function: when you need to select more than 1 line in the
$cnx = $this->conn;
if (!$cnx || $this->status_fatal) {
echo 'GetAll -> Connection BDD failed';
80. die();
}
Top
while($data = mysql_fetch_assoc($cur)) {
array_push($return, $data);
}
90.
return $return;
}
if ($cur == FALSE) {
$ErrorMessage = @mysql_last_error($cnx);
$this->handleError($query, $ErrorMessage);
105. }
else {
$this->Error=FALSE;
$this->BadQuery="";
$this->NumRows = mysql_affected_rows();
110. return;
}
@mysql_free_result($cur);
}
<?php
include('db.class.php'); // call db.class.php
$bdd = new db(); // create a new object, class db()
With this Object-Oriented connection, we have 2 ways to do it: 1 line selection or multi-line selection
<?php
$User = $bdd->getOne('SELECT id, firstname, lastname FROM users WHERE lastname = "Smith"');
echo $User['id'].'<br>'; // display the id
echo $User['firstname'].'<br>'; // display the first name
5. echo $User['lastname']; // display the last name
Top
<?php
$Users = $bdd->getAll('SELECT id, firstname, lastname FROM users'); // select ALL from users
Now you want to insert a new member in your table users . This new member's name is: first name:
firstname1 , last name: lastname1 .
<?php
$query = $bdd->execute('INSERT INTO users (firstname, lastname) VALUES ("firstname1", "lastname1"
You want to update the last member inserted and change his first name to firstname2 and his last name to
lastname2 .
<?php
$query = $bdd->execute('UPDATE users SET firstname="firstname2", lastname="lastname2" WHERE id=20
Conclusion
Now you are able to manage your database in a simple way.
Leave a comment
Your Name or Pseudo Top
Your comment
</>
Submit Comment
include('db.class.php');
Top
function marquee_art(){
foreach($marquee as $events_sql_detail)
{
$output.=$events_sql_detail['title'];
}
$output;
$stmt = $dbh->prepare("SELECT * FROM animals WHERE animal_id = :animal_id AND animal_name = :an
Your class called db is declared twice... You have to check in your code page where.
Suraj shrestha Posted on November 17, 2015
And one thing I want to include that I called both the pages in a single page index.php using
require as
require 'class.show.php';
require 'class.add.php';
require 'class.database.php';
$bdd = new db();
to setup a proper message. I didn't check because normally my queries are correct :)
).
Or isn't it necessary?