使用sql.Load(vm)启用数据库功能
package main
import (
"fmt"
"github.com/php-any/database/sql"
"os"
_ "github.com/go-sql-driver/mysql" // 使用 mysql
"github.com/php-any/origami/parser"
"github.com/php-any/origami/runtime"
"github.com/php-any/origami/std"
)
func main() {
// 创建解析器
p := parser.NewParser()
// 创建程序运行的环境
vm := runtime.NewVM(p)
std.Load(vm)
sql.Load(vm)
// 获取脚本路径参数
scriptPath := os.Args[1]
// 检查文件是否存在
if _, err := os.Stat(scriptPath); os.IsNotExist(err) {
_, _ = fmt.Fprintf(os.Stderr, "错误: 文件 '%s' 不存在\n\n", scriptPath)
os.Exit(1)
}
_, err := vm.LoadAndRun(scriptPath)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "错误: %v\n", err)
if !parser.InLSP {
panic(err)
}
}
}
namespace test;
use database\sql\open;
$db = open("mysql", "root:root@/temp");
$db->ping();
$rows = $db->query("SELECT id, age, name, coin, create_at FROM users");
for ;$rows->next(); {
$rows->scan($id, $age, $name, $coin, $create_at);
echo "id: {$id}, age: {$age}, name: {$name}, coin: {$coin}, create_at: {$create_at}\n";
}