3
- Qoşuldu
- 10 Sen 2022
- Mesajlar
- 235
- Reaksiya hesabı
- 117
- Xallar
- 43
bu meqalede php oop sistemi ile hazirlanan snippet-i paylasmaq qerarina geldim.
hemin snippet-in istifade qaydasini bu movzuda etrafli sekilde izah edecem.
bunu da qeyd edim ki, paylasdigim db class mysqli ile yazildigi ucun sql injectiondan tam mudafie olunub.
indi ise hemin classin istifade qaydasina kecek.
bu qeder umid edirem ki yazdigim meqale herkes ucun faydali olacaq.
In this article, I decided to share the snippet prepared with the php oop system.
I will explain the usage of these snippet in detail in this topic.
I should also note that the db class I shared is fully protected from SQL injection because it is written with mysqli.
Now lets will go to the rules of use of that class.
Or
That's all, I hope that what I wrote will be useful for everyone.
hemin snippet-in istifade qaydasini bu movzuda etrafli sekilde izah edecem.
bunu da qeyd edim ki, paylasdigim db class mysqli ile yazildigi ucun sql injectiondan tam mudafie olunub.
- asagida paylasdigim linkden db.txt faylinin adini db.php ile evez edin.
- hemin sozu geden db.php faylini asagidaki kod vasitesile oz proyektinize samil edin.
PHP:
require_once __DIR__ . '/db.php';
indi ise hemin classin istifade qaydasina kecek.
- MySQL database-a qosulmaq ucun asagidaki kodu elave edin ve $db ile baslayan deyiskenlere oz database melumatlarinizi qeyd edin.
PHP:
include 'db.php';
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'example';
$db = new db($dbhost, $dbuser, $dbpass, $dbname);
- teyin olunan serte esasen database-dan muvafiq setri secmek ucun asagidaki koddan istifade ede bilersiniz.
PHP:
$account = $db->query('SELECT * FROM accounts WHERE username = ? AND password = ?', 'test', 'test')->fetchArray();
echo $account['name'];
- ve ya
PHP:
$account = $db->query('SELECT * FROM accounts WHERE username = ? AND password = ?', array('test', 'test'))->fetchArray();
echo $account['name'];
- foreach dongu vasitesile birden cox setrleri secmek ucun ise asagidaki kodu istifade ede bilersiniz.
PHP:
$accounts = $db->query('SELECT * FROM accounts')->fetchAll();
foreach ($accounts as $account) {
echo $account['name'] . '<br>';
}
- database datanin array olaraq saxlanilmasini istemirsinizse, asagidaki kodu callback olaraq da istifade ede bilersiniz.
PHP:
$db->query('SELECT * FROM accounts')->fetchAll(function($account) {
echo $account['name'];
});
- database datanin sayini elde etmek ucun ise asagidaki kodu istifade ede bilersiniz.
PHP:
$accounts = $db->query('SELECT * FROM accounts');
echo $accounts->numRows();
- insert, update kimi CRUD emeliyyatlarinda mysql sorguya esasen tapilan setrin movcud olub-olmadigini yoxlamaq ucun asagidaki kodu istifade ede bilersiniz.
PHP:
$insert = $db->query('INSERT INTO accounts (username,password,email,name) VALUES (?,?,?,?)', 'test', 'test', '[email protected]', 'Test');
echo $insert->affectedRows();
- sorgularin umumi sayini elde etmek ucun asagidaki kodu istifade ede bilersiniz.
PHP:
echo $db->query_count;
- insert CRUD emeliyyat neticesinde son auto increment deyerini elde etmek ucun asagidaki kodu istifade ede bilersiniz.
PHP:
echo $db->lastInsertID();
- database-ni sonlandirmaq ucun ise asagidaki kodu istifade ede bilersiniz.
PHP:
$db->close();
bu qeder umid edirem ki yazdigim meqale herkes ucun faydali olacaq.
In this article, I decided to share the snippet prepared with the php oop system.
I will explain the usage of these snippet in detail in this topic.
I should also note that the db class I shared is fully protected from SQL injection because it is written with mysqli.
- Change the name of the db.txt file to db.php from the link I shared below.
- Add the following db.php file to your project using the code below.
PHP:
require_once __DIR__ . '/db.php';
Now lets will go to the rules of use of that class.
- To connect to the MySQL database, add the code below and add your database information to the variables starting with $db.
PHP:
include 'db.php';
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'example';
$db = new db($dbhost, $dbuser, $dbpass, $dbname);
- You can use the following code to select the corresponding row from the database.
PHP:
$account = $db->query('SELECT * FROM accounts WHERE username = ? AND password = ?', 'test', 'test')->fetchArray();
echo $account['name'];
Or
PHP:
$account = $db->query('SELECT * FROM accounts WHERE username = ? AND password = ?', array('test', 'test'))->fetchArray();
echo $account['name'];
- You can use the code below to select more than one row using the foreach clause.
PHP:
$accounts = $db->query('SELECT * FROM accounts')->fetchAll();
foreach ($accounts as $account) {
echo $account['name'] . '<br>';
}
- If you don't want the database data to be stored as an array, you can use the following code as a callback.
PHP:
$db->query('SELECT * FROM accounts')->fetchAll(function($account) {
echo $account['name'];
});
- You can use the following code to get the number of database data.
PHP:
$accounts = $db->query('SELECT * FROM accounts');
echo $accounts->numRows();
- In CRUD operations such as insert, update you can use the code below to check whether the table row found in the mysql query is available.
PHP:
$insert = $db->query('INSERT INTO accounts (username,password,email,name) VALUES (?,?,?,?)', 'test', 'test', '[email protected]', 'Test');
echo $insert->affectedRows();
- You can use the code below to get the total number of queries.
PHP:
echo $db->query_count;
- You can use the following code to get the last auto increment value as a result of insert CRUD operation.
PHP:
echo $db->lastInsertID();
- To terminate the database, you can use the following code.
PHP:
$db->close();
That's all, I hope that what I wrote will be useful for everyone.