?php
class SwooleMysql {
public $db = "";
public $server = [];
public function __construct() {
//實例化
$this->db = new swoole_mysql();
//配置參數(shù)
$this->server = [
'host' => '127.0.0.1',
'port' => 3306,
'user' => 'root',
'password' => 'xxooni',
'database' => 'lws',
'charset' => 'utf8',
'timeout' => 2, // 可選:連接超時時間(非查詢超時時間),默認(rèn)為SW_MYSQL_CONNECT_TIMEOUT(1.0)
];
}
//執(zhí)行SQL語句操作
public function execute($sql) {
$this->db->connect($this->server, function($db, $res) use($sql) {
echo "連接MySQL...\n";
if($res === false) {
var_dump($db->connect_errno, $db->connect_error);
die;
}
$db->query($sql, function($db, $res){
if($res === false) {
var_dump($db->error, $db->errno);
}elseif($result === true) {
var_dump($db->affected_rows, $db->insert_id);
}
var_dump($res);
$db->close();
});
});
return true;
}
}
$obj = new SwooleMysql();
$sql = 'select `article_title` from `lws_article` where `article_id`=66';
$res = $obj->execute($sql);
var_dump($res);
echo "lws\n";
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP擴展開發(fā)教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php curl用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》及《php字符串(string)用法總結(jié)》