0% found this document useful (0 votes)
40 views4 pages

Limbajul PHP

php

Uploaded by

doinny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views4 pages

Limbajul PHP

php

Uploaded by

doinny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

 Welcome to CodeIgniter4

 Installation

 Build Your First Application

 CodeIgniter4 Overview

 General Topics

 Controllers and Routing

 Building Responses

 Working With Databases


o Quick Start: Usage Examples
o Database Configuration
o Connecting to a Database
o Running Queries
o Generating Query Results
o Query Helper Functions
o Query Builder Class
o Transactions
o Getting MetaData
o Custom Function Calls
o Database Events
o Database Utilities

 Modeling Data

 Managing Databases

 Library Reference

 Helpers
 Testing

 Command Line Usage

 Extending CodeIgniter

Your new development career awaits. Check out the


latest listings.ADS VIA CARBON

 Docs »

 Working With Databases »

 Database Quick Start: Example Code


Database Quick Start: Example Code


The following page contains example code showing how the database class is used.
For complete details please read the individual pages describing each function.

Initializing the Database Class


The following code loads and initializes the database class based on
your configuration settings:
$db = \Config\Database::connect();

Once loaded the class is ready to be used as described below.

Note: If all your pages require database access you can connect automatically. See
the connecting page for details.

Standard Query With Multiple Results (Object Version)


$query = $db->query('SELECT name, title, email FROM my_table');
$results = $query->getResult();

foreach ($results as $row)


{
echo $row->title;
echo $row->name;
echo $row->email;
}

echo 'Total Results: ' . count($results);

The above getResult() function returns an array of objects. Example: $row->title

Standard Query With Multiple Results (Array Version)


$query = $db->query('SELECT name, title, email FROM my_table');
$results = $query->getResultArray();

foreach ($results as $row)


{
echo $row['title'];
echo $row['name'];
echo $row['email'];
}

The above getResultArray() function returns an array of standard array indexes.


Example: $row[‘title’]

Standard Query With Single Result


$query = $db->query('SELECT name FROM my_table LIMIT 1');
$row = $query->getRow();
echo $row->name;

The above getRow() function returns an object. Example: $row->name

Standard Query With Single Result (Array version)


$query = $db->query('SELECT name FROM my_table LIMIT 1');
$row = $query->getRowArray();
echo $row['name'];

The above getRowArray() function returns an array. Example: $row[‘name’]

Standard Insert
$sql = "INSERT INTO mytable (title, name) VALUES (".$db->escape($title).", ".$db-
>escape($name).")";
$db->query($sql);
echo $db->affectedRows();

Query Builder Query


The Query Builder Pattern gives you a simplified means of retrieving data:

$query = $db->table('table_name')->get();

foreach ($query->getResult() as $row)


{
echo $row->title;
}

The above get() function retrieves all the results from the supplied table. The Query
Builder class contains a full complement of functions for working with data.

Query Builder Insert


$data = [
'title' => $title,
'name' => $name,
'date' => $date
];

$db->table('mytable')->insert($data); // Produces: INSERT INTO mytable (title, name, date)


VALUES ('{$title}', '{$name}', '{$date}')

Next  Previous

© Copyright 2019-2020 CodeIgniter Foundation Last updated on Jul 15, 2020.


Built with Sphinx using a theme provided by Read the Docs.

You might also like