MySql Database Connection
Required Namespace
  • Ness
    • Autopulse
      • MySqlConnect

You can include this library to your code file by typing the below code;

use Ness\Autopulse\MySqlConnect


Getting Started #top

Ness PHP Framework uses libraries derived from PHP's PDO library for database operations. There are currently classes for MySQL databases that are frequently preferred by developers. However, Ness PHP supports all databases supported by the PHP PDO plugin. All you need to do is specify the required connection string at the time the class is derived. This article is followed by articles describing all these operations and showing the use of the functions.You can use MySql connections with the MySqlConnect class and use the dbConnect class for all other PDO-enabled transactions.Once you have successfully defined your connection, you can run your queries using the MySqlCommand or dbCommand + dbConnect classes.


Using MySql Database #top

This library is used to manage your mysql connections.

Create a Connection

You can create your connection in construct when you define your database variable.

                                    $connection = new MySqlConnect($host, $user, $pass, $db, $options);
                                
Create a new connection
Return Object
Parameters $host String; Connection Host
$user String; Username of connection
$pass String; Password for connection
$db String; Database name
$options Array Optional;PHP PDO Options



Connection Source #top

This function is used to return an active database connection.

                                $connection->Source();
                            
Get an active connection for using with MySqlCommand class.
Return PDO Connection
Parameters None



Check Connection #top

Check if connection is available

                                $connection->isConnected();
                            
Check for active connection
Return Boolean
Parameters None



Error List #top

Show last connection errors.

                                $connection->lastError();
                            
Check for last connection error
Return String
Parameters None



Reconnect to Database #top

Reconnect to active database.

                                $connection->Connection($host, $user, $pass, $db, $options);
                            
Create a new connection
Return Object
Parameters $host String; Connection Host
$user String; Username of connection
$pass String; Password for connection
$db String; Database name
$options Array Optional;PHP PDO Options



Close an active Connection #top

Close connection to active database.

                                $connection->Close();
                            
Close a connection
Return Boolean
Parameters None






Example #top

Lets create a connection to a database called blogdb and print message if connection is available;

                                    
use Ness\Controller as myController;
use Ness\Autopulse\MySqlConnect;


class indexController extends myController
{
    public function indexAction($param = 0)
    {     
        $con_str = array("host"=>"localhost",
                            "password"=>"",
                            "user"=>"root",
                            "database-name"=>"blogdb");

        $con = new MySqlConnect($con_str["host"], 
                                $con_str["user"], 
                                $con_str["password"], 
                                $con_str["database-name"]);
        
        if($con->isConnected()){
            echo "Connection Available";
        }else{
            echo "Connection Error -: ".$con->lastError();
        }

    }
}