Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class connect{
- public $db_host= "localhost"; //database host e.g. localhost,127.0.0.1
- public $username="admin"; //username of sql server
- public $password= "pass"; //password of sql server
- public $db_name="dbtest"; //name of existing database on server
- private $dbc=null; //database identifier
- function __construct(){
- try{
- $this->dbc= new PDO("mysql:host=$this->db_host; dbname=$this->db_name", $this->username, $this->password);
- }
- catch (PDOException $e){
- $this->dbc=null;
- echo 'Connection failed: ' . $e->getMessage();
- exit();
- }
- }
- /*
- * To run this tester, make sure your database contains a table called users with a field called email with at least one value
- *
- */
- public function tester2(){
- $result = $this->dbc->query("SELECT * FROM `users` ", PDO::FETCH_ASSOC);
- foreach($result as $row) {
- var_dump($row);
- }
- }
- /*
- * You do not need any table to test this one
- */
- public function tester(){
- $result = $this->dbc->query("SELECT 1+1 FROM `users` ", PDO::FETCH_ASSOC);
- foreach($result as $row) {
- var_dump($row);
- }
- }
- }
- $connection = new connect();
- $connection->tester();
- $connection->tester2();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement