Menu

Documentation

Gowtham Vasishta

Documentation

Create a database (say 'Prober') and a table (say 'sample')

Creation of database

Before, we move to Prober ! let us create a database 'prober'.
1. create database 'prober' in PHPMysql.
OR
2. Run the following sql query in mysql console.

CREATE DATABASE 'prober';
Creation of table

Now let's create a table 'sample'
Before, we move to Prober ! let us create a database 'prober'.
1. create table 'sample' in PHPMysql (using specifications provided in below SQL query).
OR
2. Run the following sql query in mysql console.

CREATE TABLE IF NOT EXISTS `sample` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`content` varchar(255) NOT NULL,
 `author` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=41 ;

Initiating object of Prober class

Prober class fires a constructor , which directly connects to database. So, its obvious that we need to provide database credentials (localhost, username, password, database_name ) while initiating an object of Prober class.

$pb = new prober("localhost", "username", "password", "database_name" );

For example

$pb = new prober("localhost", "gowtham", "XXXXXXX", "prober" );

Set the database table

Now set the database table, that you wanna handle. Our table is 'sample' !

$pb->db_table("sample");

Insert operation

Before we do any operation on database , we need an object of the method ' operate () '.

$handler = $pb->operate(); 

Now, let us insert a row into our database. our database has four fields (id, title, content, author). We have set 'id' under auto_increment & primary key. So, now we need to deal title, content & author.

$pb = new prober("localhost", "gowtham", "XXXXXXX", "prober" );
$pb->db_table("sample");
$handler = $pb->operate();
    //directly refer to database fields using $handler object
    $handler->title = "Java";
    $handler->content = "Java is a programming language";
    $handler->author = "Gowtham Vasishta";
$pb->insert();
  • First of all, we created a $handler object to deal database fields.
  • Then we directly assigned values to referenced database field object.
  • Then we called ' insert() ' function using ' $pb ' object (instead of $handler !).
  • And, that's all ! values are inserted into your table.

Now, let us insert some more rows !!

$handler = $pb->operate();
    //directly refer to database fields using $handler object
    $handler->title = "C++";
    $handler->content = "C++ is a programming language";
    $handler->author = "myself";
$pb->insert();

$handler = $pb->operate();
    //directly refer to database fields using $handler object
    $handler->title = "C";
    $handler->content = "C is a programming language";
    $handler->author = "myself";
$pb->insert();

Select operation

Suppose I need to select the row with id = 1; i.e. similar to sql statement

SELECT * FROM 'sample' WHERE id = 1;

Using prober it can done with following code

$handler = $pb->operate();
    //directly refer to database fields using $handler object
    // here referring where condition i.e. where id = 1 
    $handler->id = 1;
$result = $pb->select();

echo $result->id ."<br/>". $result->title ."<br/>". $result->content ."<br/>". $result->author ;

So, output would be

1
Java
Java is a programming language
Gowtham Vasishta
  • Create an handler & refer the where condition i.e. $handler->id = 1
  • Call select operation using ' $pb ' object.
  • Store the result in ' $result '.
  • If you select a single row, then ' select() ' function returns a object.
  • If you select multi rows, then ' select() ' function returns array of objects.
  • If there are no rows found for your ' select() ' operation then it returns false.

So, now let us see "how to execute if there are multi rows ?"

$handler = $pb->operate();
    //directly refer to database fields using $handler object
    // here referring where condition i.e. where id = 1 
    $handler->author = "myself";
$result = $pb->select();

//rows with id = 2 and id = 3 have author = "myself". So it returns array of objects

foreach( $result as $key => $row ) {
        echo $result->id;
        echo "<br/>";
}

So, now output would be

2
3

Suppose you need to select title, content from the table 'sample' where id = 1 i.e. the consequent sql statement would be

SELECT title, content FROM 'sample' WHERE id = 1;

It can be implemented in prober in following way !

$handler = $pb->operate();
    //directly refer to database fields using $handler object
    // here referring where condition i.e. where id = 1 
    $handler->id = 1;
//refer those fields in select method that you wanna select !
$result = $pb->select("title, content");

echo  $result->title ."<br/>". $result->content ;

So, output would be

Java
Java is a programming language
  • Here, we gave the required fields as arguments to the ' select () ' method !
    Suppose, you need to select rows on multiple where conditions, like we need to select row where title = c and author = myself (yes ! its row with id = 3). The consequent sql statement is .
SELECT * FROM 'sample' WHERE title = 'c' and author = 'myself';

Now, let's see the implementation in prober (its too easy !)

$handler = $pb->operate();
    //directly refer to database fields using $handler object
    // here referring where condition i.e. where title = "C" & author = "myself"
    $handler->title = "C";
    $handler->author = "myself";
$result = $pb->select();

//row with id = 3 have title = "C" & author = "myself". So it returns a object
echo $result->id;

So, now output would be

3

Update operation

Suppose I want to update author as 'Gowtham' & change the content at row with id = 3. The subsequent sql statement is

UPDATE 'sample' SET author = 'Gowtham', content = 'C++ is a oop language' WHERE id = 3;

Now, let's see how to implement it in prober.

$handler = $pb->operate();
    //directly refer to database fields using $handler object
    // here referring set condition i.e. author = 'Gowtham' & content = "C is a procedural language"
    $handler->author = "Gowtham";
    $handler->content = "C is a procedural language";
//specify the where condition i.e. id = 3 as argument for update() method
$result = $pb->update("id = 3");

Now let us perform a select() operation on row with id = 3. And see the output

$handler = $pb->operate();
    $handler->id = 3;
$result = $pb->select();

echo $result->id ."<br/>". $result->title ."<br/>". $result->content ."<br/>". $result->author ;

So, output would be

3
C
C is a procedural language
Gowtham

Delete operation

The delete() method can perform two types of delete operations. They are
* or - delete (default)
* and - delete (need to mention as argument !)

Suppose I need to delete all the rows having author as 'myself' or title as 'Java'. The subsequent sql statement is

DELETE FROM 'sample' WHERE author = 'myself' OR title = 'Java';

Then prober implementation is

$handler = $pb->operate();
    //directly refer to database fields using $handler object
    $handler->author = "myself";
    $handler->title = "Java";

$result = $pb->delete();

And, suppose I need to delete all the rows having author as 'myself' and title as 'Java'. The subsequent sql statement is

DELETE FROM 'sample' WHERE author = 'myself' AND title = 'Java';

Then prober implementation is

$handler = $pb->operate();
    //directly refer to database fields using $handler object
    $handler->author = "myself";
    $handler->title = "Java";

$result = $pb->delete("AND");

Execute operation

To provide flexibility , prober has got a ' execute() ' method which can execute sql statements and give out the either raw result or processed result.

Suppose I need to see the all the rows of the table 'sample' (well you can select() operation !), let us see how we can carry out using execute method and raw result.

// To get raw output
$pb = new prober("localhost", "gowtham", "XXXXXXX", "prober" );
$pb->db_table("sample");

$sql = "SELECT * FROM 'sample'";
$result = $pb->execute($sql);

while( $row = $result->fetch_assoc()) {
    print_r ($row);
    echo "<br/>";
}

Now, let us concentrate on getting processed result. That is the raw result is processed
* if a single column is operated, then it returns a object
* if multi column is operated, then it returns a array of objects
To sanitize the raw result , we need to extra argument 'true' to ' execute() ' function

// To get processed output
$pb = new prober("localhost", "gowtham", "XXXXXXX", "prober" );
$pb->db_table("sample");

$sql = "SELECT * FROM 'sample'";
$result = $pb->execute($sql, true);

// since there are more then one row, so it returns array of objects
foreach($result as $key => $row ) {
    echo $row->id;
    echo "<br/>";
    echo $row->title;
    echo "<br/>";
    echo $row->content;
    echo "<br/>";
    echo $row->author;
}


Related

Wiki: Home

MongoDB Logo MongoDB
Gen AI apps are built with MongoDB Atlas
Atlas offers built-in vector search and global availability across 125+ regions. Start building AI apps faster, all in one place.
Try Free →