PHP code example of migro / php-core-migration
1. Go to this page and download the library: Download migro/php-core-migration library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
migro / php-core-migration example snippets
$config = [
"host" => "localhost",
"username" => "root",
"password" => "",
"database" => "php_code_first_approach_db",
];
function connectToDatabase($config)
{
$conn = mysqli_connect($config['host'], $config['username'], $config['password'], $config['database'])or die("lost");
if (!$conn) {
die('Connection failed: ' . mysqli_connect_error());
}
return $conn;
}
class BaseMigration {
public static function createTable($conn, $sql) {
if ($conn->query($sql) === TRUE) {
echo "Table created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
}
}
class RoleMigration extends BaseMigration
{
public static function up($conn)
{
$sql = "SHOW TABLES LIKE 'tbl_userRoles'";
$result = $conn->query($sql);
if ($result->num_rows == 0) {
$sql = "CREATE TABLE tbl_userRoles (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)";
self::createTable($conn, $sql);
echo "tbl_userRoles has created";
}else{
echo "tbl_userRoles already exist";
}
}
}
re_once '../config/connection.php';
_once 'UserMigration.php';
$conn = connectToDatabase($config);
$roleMigration = RoleMigration::up($conn);
$userMigration = UserMigration::up($conn);
http://localhost/your_project_directory/database/migrations/Migrate.php