MySQLiLib
๊ฐ๋ณ๊ฒ, ๊ฐ๋จํ ์ค๋ฌด์ ์ ์ฉํ ์ ์๋ MySQL ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค., (*1)
์ฌ์ฉํด๋ณด์๊ณ ์ด์์์ผ๋ฉด ๋ฉ์ผ์ฃผ์ธ์~ ์ธ์ ๋ ์ง ๋ฌธ์ ํ์์
๋๋ค., (*2)
php5.6 ~ php8.2 ๊น์ง ์ฌ์ฉ๊ฐ๋ฅํฉ๋๋ค., (*3)
Install
$ composer require jonathanbak/mysqlilib
$ composer install
Test
phpunit.xml.dist ์ ์๋ ๋ด์ฉ์ ๋ณธ์ธ์ MySQL ์๋ฒ ์ ๋ณด๋ฅผ ๋ฃ๊ณ , (*4)
<php>
<var name="DB_HOST" value="localhost" />
<var name="DB_USER" value="test" />
<var name="DB_PASSWD" value="test1234" />
<var name="DB_NAME" value="db_test" />
<var name="DB_PORT" value="3306" />
</php>
phpunit ์คํํ์ฌ ํ
์คํธ ํด๋ด
๋๋ค., (*5)
$ vendor/bin/phpunit
Usage
๊ฐ๋จํ ๋๋น ์ฐ๊ฒฐ ๋ฐ SELECT ์ฟผ๋ฆฌ :, (*6)
$DB = new MySQLiLib($host, $user, $password, $dbName);
$query = "SELECT * FROM test";
$row = $DB->fetch($query);
var_dump($row);
SELECT
test ํ
์ด๋ธ์ id = 222 ์ธ ๋ฐ์ดํฐ ํ ํ ๊ฐ์ ธ์ค๊ธฐ, (*7)
$query = "SELECT * FROM test WHERE id = ?";
$row = $DB->fetch($query, array(222));
var_dump($row);
test ํ
์ด๋ธ์ id = 11 ์ธ ๋ฐ์ดํฐ ์ฌ๋ฌ ํ ๊ฐ์ ธ์ค๊ธฐ, (*8)
$query = "SELECT * FROM test WHERE id = ?";
$rows = array();
while($row = $DB->fetch($query, array(11))){
$rows[] = $row;
}
var_dump($rows);
test ํ
์ด๋ธ์ name LIKE 'ํ
์คํธ%' ์ธ ๋ฐ์ดํฐ ์ฌ๋ฌ ํ ๊ฐ์ ธ์ค๊ธฐ, (*9)
$query = "SELECT * FROM test WHERE name LIKE '??%'";
$rows = array();
while($row = $DB->fetch($query, array('ํ
์คํธ'))){
$rows[] = $row;
}
var_dump($rows);
INSERT, UPDATE, DELETE
$query = "INSERT INTO test SET id = ?, reg_date = ?";
$result = $DB->query($query, array(33, date("Y-m-d H:i:s")));
var_dump($result);
$query = "DELETE FROM test SET id = ?";
$result = $DB->query($query, array(33));
var_dump($result);
Exception
test ํ
์ด๋ธ์ id = 33 ์ธ ๋ฐ์ดํฐ๊ฐ ์ด๋ฏธ ์
๋ ฅ๋์์๋ Duplicate entry '33' for key 'PRIMARY' ์ค๋ฅ ๋ฐ์์, (*10)
try{
$query = "INSERT INTO test SET id = ?, reg_date = ?";
$result = $DB->query($query, array(33, date("Y-m-d H:i:s")));
}catch(\MySQLiLib\Exception $e){
//print error message "Duplicate entry '33' for key 'PRIMARY'"
var_dump($e->getMessage());
}
Prepared statement query
$query = "INSERT INTO test SET id = ?, reg_date = ?";
$DB->bind_param('i');
$result = $DB->query($query, array(33, date("Y-m-d H:i:s")));
var_dump($result);
$query = "DELETE FROM test SET id = ?";
$DB->bind_param('i');
$result = $DB->query($query, array(33));
var_dump($result);