Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Database related functions in CakePHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 310
    Comment on it

    Database is used to collect information, which is organized in a proper way so that it can be accessed, managed and updated easily.

    CakePHP supports following database servers:

    • MySQL 5.1+
    • SQLite 3
    • PostgreSQL 8+
    • SQLServer 2008+

     

    1. Create Database:

    We can create database easily bt using DSN string.

     

    Syntax:-

    use Cake\Datasource\ConnectionManager;
    
    $dsn = 'mysql://root:password@localhost/my_database';
    ConnectionManager::config('default', ['url' => $dsn]);

    Access the connection:

    $connection = ConnectionManager::get('default');

     

    2. Run SELECT Statement:

    Select query is used to select elements from a particular table of the database.

     

    Syntax of using select query:-

    use Cake\Datasource\ConnectionManager;
    
    $connection = ConnectionManager::get('default');
    $results = $connection->execute('SELECT * FROM users')->fetchAll('assoc');

     

    3. Run INSERT Statement:

    Insert query is used to add/insert elements in a table of database.

     

    Syntax of using Insert query:-

    use Cake\Datasource\ConnectionManager;
    
    $connection = ConnectionManager::get('default');
    $connection->insert('users', [
        'title' => 'A New Article',
        'created' => new DateTime('now')
    ], ['created' => 'datetime']);

     

    4. Run UPDATE Statement:

    Update statement is used to edit elements in a table of database.

     

    Syntax of using Update query:-

    use Cake\Datasource\ConnectionManager;
    $connection = ConnectionManager::get('default');
    $connection->update('users', ['title' => 'New title'], ['id' => 10]);

     

    5. Run DELETE Statement:

    Delete query is used to delete particular row, column or whole table of a database.

     

    Syntax of using Delete query:-

    use Cake\Datasource\ConnectionManager;
    $connection = ConnectionManager::get('default');
    $connection->delete('users', ['id' => 10]);

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: