လက်ရှိတည်နေရာ: ပင်မစာမျက်နှာ> နောက်ဆုံးရဆောင်းပါးများစာရင်း> MySQLI :: stmt_init function ပါ 0 င်သည့်ဒေတာဘေ့စ်စစ်ဆင်ရေးနည်းလမ်းကိုယူနစ်သို့မည်သို့စမ်းသပ်ရမည်နည်း။

MySQLI :: stmt_init function ပါ 0 င်သည့်ဒေတာဘေ့စ်စစ်ဆင်ရေးနည်းလမ်းကိုယူနစ်သို့မည်သို့စမ်းသပ်ရမည်နည်း။

M66 2025-05-17

PHP ဖွံ့ဖြိုးတိုးတက်မှုတွင်ဒေတာဘေ့စ်လုပ်ငန်းများအတွက် MySQLI extension များကိုသုံးလေ့ရှိသည်။ MySQLI :: stmtt_init function သည် SQL Query တို့၏လုံခြုံရေးနှင့်ထိရောက်မှုကိုတိုးတက်စေရန်အလွန်အရေးကြီးသည်။ ၎င်းသည် sql မေးမြန်းချက်များကိုတိုးတက်စေရန်အလွန်အရေးကြီးသည်။ Database connection များတည်ရှိမှုသည်ဒေတာဘေ့စ ဆက်သွယ်မှုတည်ရှိမှုကြောင့်,

ဤဆောင်းပါးသည် PHP တွင်ယူနစ်စစ်ဆေးမှုကိုမည်သို့မိတ်ဆက်ပေးမည်, အထူးသဖြင့် MySQLI :: stmt_init ပါ 0 င်သည့်ဒေတာဘေ့စ်စစ်ဆင်ရေးနည်းစနစ်များကိုမည်သို့စမ်းသပ်ရမည်ကိုမိတ်ဆက်ပေးမည်။ PHP ၏ယူနစ်စစ်ဆေးခြင်းမူဘောင် - PHPunit ကိုအသုံးပြုပါမည်။

1 ။ ဒေတာဘေ့စ်စစ်ဆင်ရေးအတန်းတစ်ခုဖန်တီးပါ

ပထမ ဦး စွာကျွန်ုပ်တို့သည် MySQLI :: stmt_init ကို သုံး. ဒေတာဘေ့စ်စစ်ဆင်ရေးနည်းလမ်းပါ 0 င်သည့်ရိုးရှင်းသောဒေတာဘေ့စ်စစ်ဆင်ရေးသင်တန်းကိုဖန်တီးသည်။ Code pegager ်ဌာန်းချက်မှာအောက်ပါအတိုင်းဖြစ်သည် -

 <?php
class Database
{
    private $connection;

    public function __construct($host, $username, $password, $dbname)
    {
        $this->connection = new mysqli($host, $username, $password, $dbname);
        if ($this->connection->connect_error) {
            die("Connection failed: " . $this->connection->connect_error);
        }
    }

    public function prepareAndExecute($query, $params)
    {
        $stmt = $this->connection->stmt_init();

        if ($stmt->prepare($query)) {
            $stmt->bind_param(...$params);
            $stmt->execute();
            return $stmt->get_result();
        } else {
            throw new Exception('Query preparation failed');
        }
    }
}

ဤအတန်းတွင် MySQLI :: stmt_init function ကိုအသုံးပြုသည်။

2 ။ ယူနစ်စစ်ဆေးမှုများရေးပါ

ဤဒေတာဘေ့စ်စစ်ဆင်ရေးသင်တန်းကိုစမ်းသပ်ရန်အတွက် MySQLI :: stmt_init function နှင့်ဆက်စပ်ဒေတာဘေ့စ်လုပ်ငန်းများအားကျွန်ုပ်တို့တုပရန်လိုအပ်သည်။ စမ်းသပ်မှုများပြုလုပ်စဉ်စစ်မှန်သောဒေတာဘေ့ ်အပြန်အလှန်အကျိုးသက်ရောက်မှုများကိုရှောင်ရှားရန် Phpunit နှင့် Mockery ကိုဖန်တီးရန် Phpunit နှင့် Mockery ကို အသုံးပြုသည်။

ဤတွင်ယူနစ်စမ်းသပ်ကုဒ်ဖြစ်ပါတယ်:

 <?php
use PHPUnit\Framework\TestCase;
use Mockery;

class DatabaseTest extends TestCase
{
    private $db;
    private $mockConnection;

    public function setUp(): void
    {
        $this->mockConnection = Mockery::mock('mysqli');
        $this->db = new Database('localhost', 'user', 'password', 'test_db');
        $this->db->connection = $this->mockConnection;
    }

    public function testPrepareAndExecute()
    {
        // Mock stmt_init to return a mocked statement object
        $mockStmt = Mockery::mock('mysqli_stmt');
        $this->mockConnection->shouldReceive('stmt_init')->once()->andReturn($mockStmt);

        // Mock the prepare method to return true
        $mockStmt->shouldReceive('prepare')->once()->andReturn(true);
        $mockStmt->shouldReceive('bind_param')->once();
        $mockStmt->shouldReceive('execute')->once();
        $mockStmt->shouldReceive('get_result')->once()->andReturn(true);

        // Call the method
        $result = $this->db->prepareAndExecute('SELECT * FROM users WHERE id = ?', ['i', 1]);

        // Assert that the result is true
        $this->assertTrue($result);
    }

    public function tearDown(): void
    {
        Mockery::close();
    }
}

ဤစမ်းသပ်မှုတွင်ကျွန်ုပ်တို့သည် MySQLI နှင့် MySQLI_STMT အရာဝတ်ထုများကိုတုပရန် MockeryDeCute Objects ကို တုပရန်နှင့် ကြိုတင်ပြင်ဆင်ထား သည့် ပြင်ဆင်ခြင်း ,

3 ။ စမ်းသပ်မှုကို run ပါ

သင် Phpunit နှင့် Mockery install လုပ်ထားကြောင်းသေချာပါစေ။ ၎င်းကိုအောက်ပါ command မှတဆင့် install လုပ်နိုင်သည်။

 composer require --dev phpunit/phpunit mockery/mockery

ထို့နောက်သင်သည်စမ်းသပ်မှုကို run နိုင်သည်။

 php vendor/bin/phpunit DatabaseTest.php

အကယ်. စမ်းသပ်မှုလွန်သွားလျှင်အောက်ပါနှင့်ဆင်တူသည့်သတင်းစကားကိုသင်တွေ့လိမ့်မည်။

 OK (1 test, 1 assertion)

4 ။ နိဂုံးချုပ်

Mock Object နှင့် Phpunit မူဘောင်ကိုအသုံးပြုခြင်းအားဖြင့် MySQLI :: stmt_init ပါ 0 င်သည့်ဒေတာဘေ့စ်လုပ်ငန်းစဉ်ကိုထိရောက်စွာစစ်ဆေးနိုင်သည်။ ၎င်းသည်ဒေတာဘေ့စ်စမ်းသပ်မှုများရှိဒေတာဘေ့စ်၏ရရှိမှုအပေါ်မှီခိုမှုကိုရှောင်ရှားနေစဉ်ဒေတာဘေ့စ်စစ်ဆင်ရေး၏မှန်ကန်မှုကိုသေချာစေရန်ခွင့်ပြုသည်။

အထက်ဖော်ပြပါအချက်များမှာ MySQLI :: stmt_init function ပါ 0 င်သည့်ဒေတာဘေ့စ်စစ်ဆင်ရေးနည်းလမ်းကိုမည်သို့စမ်းသပ်ရမည်ကိုမိတ်ဆက်ပေးခြင်းဖြစ်သည်။ ဤဆောင်းပါးသည်သင့်အားကူညီနိုင်ပြီး PHP ယူနစ်ကိုပိုမိုထိရောက်စွာစမ်းသပ်ရန်သင့်အားကူညီနိုင်လိမ့်မည်ဟုမျှော်လင့်ပါသည်။

ဆက်စပ်လင့်ခ်များ