[PHP] 클래스를 사용해서 MySql 데이터 베이스를 간단하게 사용한다. (PHP MySqli Database Class)

2022. 8. 30. 22:34Program/PHP

[PHP] 클래스를 사용해서 MySql 데이터 베이스를 간단하게 사용한다. (PHP MySqli Database Class)

[코드 링크]

 

GitHub - ThingEngineer/PHP-MySQLi-Database-Class: Wrapper for a PHP MySQL class, which utilizes MySQLi and prepared statements.

Wrapper for a PHP MySQL class, which utilizes MySQLi and prepared statements. - GitHub - ThingEngineer/PHP-MySQLi-Database-Class: Wrapper for a PHP MySQL class, which utilizes MySQLi and prepared ...

github.com


위 링크를 통해 이동후 MsqliDb.php 파일을 열고 코드를 가져와서 사용하고자 하는 폴더에 카피해서 넣자.

 

아래의 내용은 이 클래스를 간단하게 사용하는 몇가지 방법에 대해서 옮겨왔다.

더 자세한 사용법을 확인하려면 링크 페이지의 Table of Contents 페이지를 활용하면 된다.

 


1. Installation

우선 가장 먼저 해야할 것은 코드를 불러오도록 한다.

require_once ('MysqliDb.php');

2. Initalization

코드를 링크했다면 mySql database와 연결해야 한다.

database와 연결하는 구문은 아래와 같다.

$db = new MysqliDb ('host', 'username', 'password', 'databaseName');

3. Insert Query

simple example

$data = Array ("login" => "admin",
               "firstName" => "John",
               "lastName" => 'Doe'
);
$id = $db->insert ('테이블명', $data);
if($id)
    echo 'user was created. Id=' . $id;

4. Update Query

$data = Array (
	'firstName' => 'Bobby',
	'lastName' => 'Tables',
	'editCount' => $db->inc(2),
	// editCount = editCount + 2;
	'active' => $db->not()
	// active = !active;
);
$db->where ('id', 1);
if ($db->update ('users', $data))
    echo $db->count . ' records were updated';
else
    echo 'update failed: ' . $db->getLastError();

5. Select Query

select/get 함수 호출 후 값 또는 반환된 행이 $count 변수에 저장된다.

$users = $db->get('테이블명'); //contains an Array of all users 
$users = $db->get('테이블명', 10); //contains an Array 10 users

.

.

.


위의 링크를 통해 들어가면 아래의 내용들에 대한 자세한 사용방법을 확인할 수 있다.

Table of Contents

Initialization
Objects mapping
Insert Query
Update Query
Select Query
Delete Query
Insert Data
Insert XML
Pagination
Running raw SQL queries
Query Keywords
Where Conditions
Order Conditions
Group Conditions
Properties Sharing
Joining Tables
Subqueries
EXISTS / NOT EXISTS condition
Has method
Helper Methods
Transaction Helpers
Error Helpers
Table Locking