devnull-ir / db-php
此包已被废弃且不再维护。未建议替代包。
描述
v5.0
2023-06-03 05:27 UTC
Requires
- php: >=8.0
- ext-pdo: *
README
为 PHP 程序员提供的连接数据库(MySQL)的免费函数
本版本:5.0
连接到数据库
connect(string $dbname,string $username_db,string $password_db,string $host = 'localhost');
此函数
$cn = connect('dbName','myuser','passworduser');
类
$db = new db(string $dbname,string $username_db,string $password_db,string $host = 'localhost');
- PHP
$Option = [ PDO::ATTR_PERSISTENT => TRUE, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND =>'SET NAMES utf8', PDO::ATTR_EMULATE_PREPARES => false ]; $pdo = new PDO("mysql:host=localhost;dbname=dmn_nm;charset=utf8", "myAdmin" , 'abcdefgh1234' , $Option );
SQL 选择示例
- SQL
select * from db where id = 10 limit 3
- PHP
$select = select($cn, '*','db',['id'=>10],'limit 3');
OR
select * from db where id = 10 coin >= 15
- PHP
select($cn, '*','db',['id'=>10,['coin','>=',15]]);
- 执行
array (size=2)
'count' => int 3
'fetchAll' =>
array (size=3)
0 =>
array (size=7)
'id' => int 17
'step' => string 'support' (length=7)
'chat_id' => int 1212
'Cash' => null
'vip' => int 0
'grade' => int 0
'Download' => int 0
1 =>
array (size=7)
'id' => int 18
'step' => string 'NewUser' (length=7)
'chat_id' => int 1016239559
'Cash' => null
'vip' => int 0
'grade' => int 0
'Download' => int 0
2 =>
array (size=7)
'id' => int 19
'step' => string 'NewUser' (length=7)
'chat_id' => int -663757927
'Cash' => null
'vip' => int 0
'grade' => int 0
'Download' => int 0
- SQL
select * from db
- PHP
select($cn, '*','db');
SQL LIKE 示例
- SQL
SELECT * FROM Customers WHERE CustomerName LIKE 'a%';
- PHP
此更新:2.0
like($cn, '*','Customers',[ 'CustomerName'=>'a%' ]);
OR
- SQL
select * from db where column LIKE 'a%' and id <= 15
- PHP
like($cn, '*','db',[ 'column'=>"a%" ], [ [ 'id','<=',15 ] ]);
OR
- SQL
select * from db where column LIKE 'a%' and id = 15
- PHP
like($cn, '*','db',[ 'column'=>"a%" ], [ 'id'=>15 ]);
MIN() 示例
- SQL
SELECT MIN(Price) AS SmallestPrice FROM Products;
- PHP
select($cn, 'MIN(Price) AS SmallestPrice','Products');
SQL INNER JOIN 示例
本节将在以后更新
- SQL
SELECT Orders.OrderID, Customers.CustomerName FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
- PHP
select($cn, 'Orders.OrderID, Customers.CustomerName','Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID');
SQL 插入示例
- SQL
insert into table (one, tow, there) values ('one', 'tow', 'there')
- php
insert($cn, 'table',['one'=>'one','tow'=>'tow','there'=>'there']);
- execute => false 或 true 查询
insert('table',['one'=>'one','tow'=>'tow','there'=>'there']);
insert into table (one, tow, there) values (?, ?, ?)
然后使用 prepare 和 bindValue 填充内容
SQL 删除数据
deleted(string $table ,$where = "None",string $other = null);
- SQL
DELETE FROM one WHERE p = 12
- PHP
deleted($cn, 'one',['p'=>12]);
SQL 更新示例
- SQL
update tb set id = '12' where name = '14'
- PHP
update($cn, 'tb',['id'=>12],['name'=>14]);
execute : false 或 true 如果 execute 等于 false,则表示未执行更新
SQL 创建新表
table(string $table,$column);
- SQL
CREATE TABLE accounts ( id int );
- php
table($cn, 'accounts',['id'=>'int']);
execute => false 或 true
SQL 设置唯一列
unique(string $table,$column);
- SQL
ALTER TABLE articles ADD UNIQUE (slug)
- php
unique($cn, 'articles',['slug']);
SQL 设置主键
primary(string $table,$column);
- SQL
ALTER TABLE accounts ADD PRIMARY KEY (token);
- php
primary($cn, 'accounts',['token']);
删除表 & 列示例
drop($table,array $columns = []);
- SQL 删除列
ALTER TABLE table DROP COLUMN column;
- PHP 删除列
drop($cn, 'table',['column']);
- SQL 删除表
DROP TABLE a,b;
- PHP 删除表
drop($cn, ['a','b']);
设置 AUTO_INCREMENT
autoIncrement(string $table, string $column);
Sql : ALTER TABLE
TableNabeCHANGE
columnName
columnName BIGINT NOT NULL AUTO_INCREMENT;
PHP
autoIncrement($cn, 'TableNabe','columnName');
PHP 类
$db->autoIncrement('TableNabe','columnName');
更新版本 3.5
位置
- 选择
- 更新
- like
- 删除
描述
- 如果您在演示中输入了另一个演示,则可以向第二个演示(强制)发送三个值
- 第一个值(第一个参数):connect 函数中指定的表中列的名称
- 第二个值(第二个参数):输入要检查的操作符类型
- 第三个值(第三个参数):要检查的值