MySql Database Commands
This library is used to run your queries.
Required Namespace
  • Ness
    • Autopulse
      • MySqlCommand

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

use Ness\Autopulse\MySqlCommand


Methods #top

Create Command
$cmd = new MySqlCommand(String $command, dbConnect | MySqlConnect  $source);
You need create an instance of MySqlCommand library for your commands. You can define your command when creating a MySqlCommand instance
Return Command Object
Parameters $command String; Command to run
$source Connection Source; MySqlConnect or dbConnect connection source; ex: $connection->Source();

Example:
                           $cmd = new MySqlCommand("select * from db", $mysqlconnection->Source());
                       


Set Parameters
$cmd->SetParameter(  String  $param,  String   $value,  $type = null)
This function is used when you use parameterized queries.
Return Void
Parameters $param String; Parameter id
$value Type; Parameter value
$type PDO Parameter type;PDO::Parameter_TYPE.

Example:
                            
$cmd = new MySqlCommand("select * from user where status=:status",$myconnnection->Source());
$cmd->setParameter(":status", "active");
$data = $cmd->FetchAll();
                            
                        


Fetch All
$cmd->FetchAll();
Fetch all records from returned query
Return Array
Parameters None

Fetch
$cmd->Fetch();
Fetch a single record from returned query
Return Array
Parameters None

Count Rows
$cmd->RowCount();
Count affected rows
Return Integer
Parameters None

Last Inserted Id
$cmd->LastInsertedID();
Return last inserted id
Return Integer
Parameters None

Execute a Command
$cmd->Execute();
Execute the command in constructor.
Return Array
Parameters None



Example: Using ObjectMappers with Database Operations

The database connections may also change during the application's development process. Instead of searching the entire code for changed information every time, you can use the ObjectMapper class to define as many connections as you want and control them from a single point.As mentioned in previous articles, the Ness PHP Framework's Register function is triggered when app starts.

Based on this information, let us define our connection. Open;
project_folder/app_config.php and add these lines to Register() function in Application class.

                            
                                
public function Register()
{
		omap::MapNew("database_1",
                        array("host"=>"localhost",
                              "database"=>"papp",
                              "user"=>"root",
                              "password"=>"myDB_password",
                              "options"=>null));
}
                            
                        


Thats all. Let's say we have a Auth controller and LoginAction, and we need to connect our database in AuthController.php controller. Open; project_folder/controller/AuthController.php and do the following;
                            
                                
use Ness\Controller As plController;
use Ness\Autopulse\MySqlConnect;
use Ness\Autopulse\MySqlCommand;
use Ness\Tool\ObjectMapper as omap;

class AuthController extends plController{

 
	public function IndexAction($param=0)
	{
           $cons = omap::GetValue("database_1");
           $myconnnection = new MySqlConnect($cons["host"],$cons["user"],$cons["password"],$cons["database"]);
           $cmd = new MySqlCommand("select * from user where status=:status",$myconnnection->Source());
           $cmd->Setparameter(":status", "active");
           $data_set = $cmd->FetchAll();
           var_dump($data_set);
            
            
            $this->View->Render("login.html");
	}
}