- mysql
$database = "we7test";
$username="root";
$password="mysql";
try{
$db = new PDO("mysql:dbname={$database};host=localhost;port=3306;charset=utf8", $username, $password);
//$db=null;//关闭连接
}catch (PDOException $e){
die($e->getMessage());
}
$sql="select * from ims_yoby_test_shop limit 10";
$rs = $db->query($sql,2);//2是关键数组,5是对象,3是索引
$data = $rs->fetch(2);//取出一条数据
while($data = $rs->fetch(PDO::FETCH_ASSOC)){//取出多条数据
$d[]=$data;
}
$d = $rs->fetchAll();//取出多条数据,功能同上,
$d = $db->exec("delete from ims_yoby_test_shop where id=1");//删除成功返回1,失败0
$d = $db->exec("update ims_yoby_test_shop set title='你好' where id=2");//修改成功返回1,失败0
$d = $db->exec("insert into ims_yoby_test_shop(title) values('标题2')");//插入成功1,失败0
$d = $db->lastInsertId();//返回插入id,和上边一起调用
try{
$db->beginTransaction();//开始一个事务
//事务内容
$db->commit();//提交事务
}catch (Exception $e){
$db->rollBack();
die($e->getMessage());
}
$sql1="select * from ims_yoby_test_shop where id in(?,?,?) ";
$rs1 = $db->prepare($sql1);//查询支持绑定,使用?或:id格式
$rs1->execute([7,10,23]);//[":id"=>1]
$d = $rs1->fetchAll(2);
$d = $rs1->fetchColumn();//返回第一行第一列单字段
$d = $rs1->fetchObject();//返回一条对象数据
$rs1->debugDumpParams();//打印SQL
$rs = $db->query($sql,2)->fetchAll();支持链式操作
- sqlite3
try{
$db = new PDO("sqlite:./s.db");
//$db=null;//关闭连接
}catch (PDOException $e){
exit($e->getMessage());
}
$sql=<<<EOF
CREATE TABLE "t" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"ip" char(15) NOT NULL,
"money" real NOT NULL
);
EOF;
$rs = $db->exec($sql);
- MongoDB
$db = new MongoDB\Driver\Manager("mongodb://localhost:27017");
//写数据
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1, 'name'=>'菜鸟教程', 'url' => 'http://www.runoob.com']);
$db->executeBulkWrite('test.sites', $bulk);
//查询
$filter = ['x' => ['$gt' => 1]];
$options = [
'projection' => ['_id' => 0],
'sort' => ['x' => -1],
];
// 查询数据
$query = new MongoDB\Driver\Query($filter, $options);
$cursor = $manager->executeQuery('test.sites', $query);
foreach ($cursor as $document) {
print_r($document);
}
作者:Yoby 创建时间:2020-07-23 19:36
更新时间:2024-12-05 13:26
更新时间:2024-12-05 13:26