本文目录一览:
php实现mysql封装类示例
php封装mysql类
复制代码
代码如下:
?php
class
Mysql
{
private
$host;
private
$user;
private
$pwd;
private
$dbName;
private
$charset;
private
$conn
=
null;
public
function
__construct()
{
$this-host
=
'localhost';
$this-user
=
'root';
$this-pwd
=
'root';
$this-dbName
=
'test';
$this-connect($this-host,$this-user,$this-pwd);
$this-switchDb($this-dbName);
$this-setChar($this-charset);
}
//负责链接
private
function
connect($h,$u,$p)
{
$conn
=
mysql_connect($h,$u,$p);
$this-conn
=
$conn;
}
//负责切换数据库
public
function
switchDb($db)
{
$sql
=
'use'
.
$db;
$this-query($sql);
}
//负责设置字符集
public
function
setChar($char)
{
$sql
=
'set
names'
.
$char;
$this-query($sql);
}
//负责发送sql查询
public
function
query($sql)
{
return
mysql_query($sql,$this-conn);
}
//负责获取多行多列的select结果
public
function
getAll($sql)
{
$list
=
array();
$rs
=
$this-query($sql);
if
(!$rs)
{
return
false;
}
while
($row
=
mysql_fetch_assoc($rs))
{
$list[]
=
$row;
}
return
$list;
}
public
function
getRow($sql)
{
$rs
=
$this-query($sql);
if(!$rs)
{
return
false;
}
return
mysql_fetch_assoc($rs);
}
public
function
getOne($sql)
{
$rs
=
$this-query($sql);
if
(!$rs)
{
return
false;
}
return
mysql_fetch_assoc($rs);
return
$row[0];
}
public
function
close()
{
mysql_close($this-conn);
}
}
echo
'pre';
$mysql
=
new
Mysql();
print_r($mysql);
$sql
=
"insert
into
stu
values
(4,'wangwu','99998')";
if($mysql-query($sql)){
echo
"query成功";
}else
{
echo
"失败";
}
echo
"br
/";
$sql
=
"select
*
from
stu";
$arr
=
$mysql-getAll($sql);
print_r($arr);
?
mysql中的一些稍微复杂用法实例代码
前言
mysql的语法相信对大家来说都不是难事,但是本文主要给分享了一些mysql复杂用法的相关内容,通过这篇文章相信大家会对mysql更深的了解一些,下面话不多说了,来一起看看详细的介绍吧
一对多数据显示成一行
GROUP_CONCAT(expr)
1、涉及的表关系:teacher表、teacher_subject_rel表(教师所能教的学科表)、subject表
2、业务场景:
需要拉取所有教师的编号(teacher_no)、学科名(subject_name)。
nbsp
教师表(teacher)和学科(teacher_subject_rel)是一对多关系,
往往查询出现的是同一教师多条
数据。我们希望得到每个教师一条数据
学科拼接成一条
1、基本语法
group_concat(
[DISTINCT]
要连接的字段
[Order
BY
排序字段
ASC/DESC]
[Separator
'分隔符']
)
2、例子
SELECT
t.teacher_id
as
'教师id',
t.teacher_no
'教师编号',
(
SELECT
GROUP_CONCAT(s.subject_name)
FROM
teacher_subject_rel
tsr
LEFT
JOIN
`subject`
s
ON
tsr.subject_id
=
s.subject_id
WHERE
t.teacher_id
=
tsr.teacher_id
)
AS
'学科'
FROM
teacher
t
子查询、查询临时表、EXISTS
例子
SELECT
*
FROM
(
SELECT
o.id,
o.student_intention_id,
s.
NAME,
s.area_id,
a.area_name,
s.exam_year,
o.
STATUS,
CASE
o.
STATUS
WHEN
'1'
THEN
'待提交'
WHEN
'2'
THEN
'待指派'
WHEN
'3'
THEN
'已完成'
WHEN
'4'
THEN
'处理中'
END
statusName,
CASE
o.emergency_degree
WHEN
'1'
THEN
'正常'
WHEN
'2'
THEN
'紧急'
WHEN
'3'
THEN
'非常紧急'
END
emergencyDegreeName,
o.emergency_degree,
o.update_time,
(
SELECT
first_lesson_time
FROM
jx_strategy
WHERE
jx_lesson_plan_order_id
=
o.id
AND
STATUS
IN
(2,
7)
AND
first_lesson_time
now()
ORDER
BY
first_lesson_time
ASC
LIMIT
1
)
AS
first_time,
(
SELECT
deal_user_id
FROM
jx_strategy
WHERE
jx_lesson_plan_order_id
=
o.id
AND
STATUS
7
AND
deal_user_id
ORDER
BY
id
DESC
LIMIT
1
)
AS
deal_user_id
FROM
jx_lesson_plan_order
o
LEFT
JOIN
student
s
ON
s.student_intention_id
=
o.student_intention_id
LEFT
JOIN
area
a
ON
s.area_id
=
a.id
WHERE
o.
STATUS
1
AND
s.phone
=
'18501665888'
AND
o.emergency_degree
=
1
AND
o.
STATUS
=
2
AND
s.exam_year
=
'2015'
AND
o.update_time
=
'2018-08-14
20:28:55'
AND
o.update_time
=
'2018-08-14
20:28:55'
)
AS
a
WHERE
1
=
1
AND
a.deal_user_id
=
145316
AND
a.first_time
=
'2018-08-17
00:00:00'
AND
a.first_time
=
'2018-08-30
00:00:00'
AND
EXISTS
(
SELECT
*
FROM
jx_strategy
js
WHERE
js.jx_lesson_plan_order_id
=
a.id
AND
js.
STATUS
IN
(2,
7)
AND
js.subject_id
IN
(2,
3)
)
ORDER
BY
a.update_time
DESC
LIMIT
0,
10
update
关联变量条件修改
1、涉及的表关系:
user_info表中的
id_number(身份证号)
teacher表中的birth字段、
关联关系usrer_id
=
teacher_id
2、业务场景:获取用户身份证上的出生日期将出生日期更新在birth字段
UPDATE
teacher
t
INNER
JOIN
(
SELECT
t.teacher_id,
t.birth,
u.id_number,
CONCAT(SUBSTRING(u.id_number,
7,
4),
'-',
SUBSTRING(u.id_number,
11,
2),
'-',
SUBSTRING(u.id_number,
13,
2))
as
birth1,
u.reg_date,
t.exit_time
from
teacher
t
INNER
JOIN
user_info
u
ON
u.user_id
=
t.teacher_id
)
info
on
info.teacher_id
=
t.teacher_id
SET
t.birth
=
info.birth1
WHERE
info.reg_date
'2018-08-20
00:00:00'
and
info.id_number
is
not
NULL
and
(info.birth
is
NULL
or
t.birth
=
'')
and
t.is_train
=
1
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。
您可能感兴趣的文章:MySQL在关联复杂情况下所能做出的一些优化Mysql一些复杂的sql语句(查询与删除重复的行)深入mysql
"ON
DUPLICATE
KEY
UPDATE"
语法的分析MySQL
最基本的SQL语法/语句MySQL与Oracle的语法区别详细对比浅析Mysql
Join语法以及性能优化MySQL
ALTER语法的运用方法MySQL
prepare语句的SQL语法MySQL进阶SELECT语法篇MySQL
SQL
语法参考
PHP同时连接多个mysql数据库示例代码
这篇文章主要介绍了PHP同时连接多个mysql数据库的具体实现,需要的朋友可以参考下
实例:
代码如下:
?php
$conn1
=
mysql_connect("127.0.0.1",
"root","root","db1");
mysql_select_db("db1",
$conn1);
$conn2
=
mysql_connect("127.0.0.1",
"root","root","db2");
mysql_select_db("db2",
$conn2);
$sql
=
"select
*
from
ip";
$query
=
mysql_query($sql);
if($row
=
mysql_fetch_array($query))
echo
$row[0]."n";
$sql
=
"select
*
from
web
";
$query
=
mysql_query($sql);
if($row
=
mysql_fetch_array($query))
echo
$row[0];
?
这段代码存在问题,在程序执行时会报错:PHP
Warning:
mysql_fetch_array()
expects
parameter
1
to
be
resource,
boolean
given
in
....
原因分析:
程序开始建立两个数据库链接,函数mysql_query()原型:
resource
mysql_query
(
string
$query
[,
resource
$link_identifier
]
)
向与指定的连接标识符关联的服务器中的当前活动数据库发送一条查询。如果没有指定
link_identifier,则使用上一个打开的连接。如果没有打开的连接,本函数会尝试无参数调用
mysql_connect()
函数来建立一个连接并使用之。查询结果会被缓存。
在本例中由于没有指定link_identifier,所以,在执行第一条sql时,默认使用的是上一个打开的链接,即$conn2,而实际上第一条sql语句应该使用的是$conn1,所以导致报错,所以为了能够链接多个mysql数据库,可以使用如下方法:
方法1:在mysql_query函数中指定所用连接,即:
代码如下:
?php
$conn1
=
mysql_connect("127.0.0.1",
"root","root","db1");
mysql_select_db("Muma",
$conn1);
$conn2
=
mysql_connect("127.0.0.1",
"root","root","db2");
mysql_select_db("product",
$conn2);
$sql
=
"select
*
from
ip";
$query
=
mysql_query($sql,$conn1);
//添加连接$conn1
if($row
=
mysql_fetch_array($query))
echo
$row[0]."n";
$sql
=
"select
*
from
web
";
$query
=
mysql_query($sql,
$conn2);
if($row
=
mysql_fetch_array($query))
echo
$row[0];
?
方法2:在sql语句中关联所用数据库,此时可以省略mysql_query的第二个参数,即:
代码如下:
?php
$conn1
=
mysql_connect("127.0.0.1",
"root","root","db1");
mysql_select_db("db1",
$conn1);
$conn2
=
mysql_connect("127.0.0.1",
"root","root","db2");
mysql_select_db("db2",
$conn2);
$sql
=
"select
*
from
db1.ip";
//关联数据库
$query
=
mysql_query($sql);
if($row
=
mysql_fetch_array($query))
echo
$row[0]."n";
$sql
=
"select
*
from
db2.web
";
$query
=
mysql_query($sql);
if($row
=
mysql_fetch_array($query))
echo
$row[0];
?