mysql 强大的trim() 函数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 强大的trim() 函数相关的知识,希望对你有一定的参考价值。
参考技术A mysql中的去除左空格函数:LTRIM(str)
Returns
the
string
str
with
leading
space
characters
removed.
以下是代码片段:
复制代码
代码如下:
mysql>
SELECT
LTRIM('
barbar');
->
'barbar'
This
function
is
multi-byte
safe.
mysql中的去除右空格函数:
RTRIM(str)
Returns
the
string
str
with
trailing
space
characters
removed.
以下是代码片段:
复制代码
代码如下:
mysql>
SELECT
RTRIM('barbar
');
->
'barbar'
This
function
is
multi-byte
safe.
trim函数可以过滤指定的字符串:
完整格式:TRIM([BOTH
|
LEADING
|
TRAILING
[remstr]
FROM]
str)
简化格式:TRIM([remstr
FROM]
str)
Returns
the
string
str
with
all
remstr
prefixes
or
suffixes
removed.
If
none
of
the
specifiers
BOTH,
LEADING,
or
TRAILING
is
given,
BOTH
is
assumed.
remstr
is
optional
and,
if
not
specified,
spaces
are
removed.
以下是代码片段:
复制代码
代码如下:
mysql>
SELECT
TRIM('
bar
');
//默认删除前后空格
->
'bar'
mysql>
SELECT
TRIM(LEADING
','
FROM
',,barxxx');
//删除指定首字符
如',‘
->
'barxxx'
mysql>
SELECT
TRIM(BOTH
','
FROM
',,bar,,,');
//删除指定首尾字符
->
'bar'
mysql>
SELECT
TRIM(TRAILING
','
FROM
'barxxyz,,');
->
'barxxyz'
复制代码
代码如下:
mysql>
UPDATE
table
SET
`field`=TRIM(TRAILING
','
FROM
`FIELD`)
WHERE
WHERE
`FIELD`
LIKE
'%,';
This
function
is
multi-byte
safe.
替换数据库中字段的最后一个分页符
复制代码
代码如下:
UPDATE
[!db.pre!]ecms_news_data_1
SET
`newstext`=TRIM(TRAILING
'[!--empirenews.page--]'
FROM
`newstext`)
WHERE
id=585;
SELECT
TRIM(TRAILING
'[!--empirenews.page--]'
FROM
`newstext`)
AS
newstex
FROM
[!db.pre!]ecms_news_data_1
WHERE
id=585;
php mysql交互
1.接受数据
if($_POST){
$uname = trim($_POST[‘uname‘]);
$pasd = md5($_POST[‘pasd‘]);
2.查询数据库
$conn = new mysqli(‘localhost‘,‘root‘,‘root‘,‘qiye‘);
$sql = "select * from admin where uname = ‘$uname‘ and pasd = ‘$pasd‘ ";
$res = $conn->query($sql);
if($conn->error){
die($conn->error);
}
$info = $res->fetch_assoc();
3. 判断数据
if($res->num_rows > 0){
setcookie(‘admin‘,$info[‘id‘]);
header(‘Location:index.php‘);
}else{
$msg= ‘账号或者密码错误‘;
}
$conn->close();
}
4.获取mysql报错信息$coon->error;
二 、文件上传
if($_FILES){
1.获取文件后缀
$temp=explode(".",$_FILES["img"]["name"]);
$extension=end($temp);
2.判断文件类型和大小
if($_FILES["img"]["name"]=="image/png" || $_FILES["img"]["name"]=="image/gif" || $_FILES["img"]["name"]=="image/jpeg" || $_FILES["img"]["name"]=="image/jpg" && $_FILES["img"]["size"]<=204800){
3.拼接文件名
$pasd = md5($_POST[‘pasd‘]);
2.查询数据库
$conn = new mysqli(‘localhost‘,‘root‘,‘root‘,‘qiye‘);
$sql = "select * from admin where uname = ‘$uname‘ and pasd = ‘$pasd‘ ";
$res = $conn->query($sql);
if($conn->error){
die($conn->error);
}
$info = $res->fetch_assoc();
3. 判断数据
if($res->num_rows > 0){
setcookie(‘admin‘,$info[‘id‘]);
header(‘Location:index.php‘);
}else{
$msg= ‘账号或者密码错误‘;
}
$conn->close();
}
4.获取mysql报错信息$coon->error;
二 、文件上传
if($_FILES){
1.获取文件后缀
$temp=explode(".",$_FILES["img"]["name"]);
$extension=end($temp);
2.判断文件类型和大小
if($_FILES["img"]["name"]=="image/png" || $_FILES["img"]["name"]=="image/gif" || $_FILES["img"]["name"]=="image/jpeg" || $_FILES["img"]["name"]=="image/jpg" && $_FILES["img"]["size"]<=204800){
3.拼接文件名
$uname = ‘../uploads‘.time().mt_rand(100,999).‘.‘.$extension;
4.存储到目录
4.存储到目录
move_uploaded_file($_FILES["img"]["tmp_name"],$uname);
5. 存入数据库
5. 存入数据库
$conn = new mysqli(‘localhost‘,‘root‘,‘root‘,‘denglu‘);
$sql = "insert into product (img) values(‘$uname‘)";
$res = $conn->query($sql);
var_dump($uname);
$conn->close();
6.调取数据库中的图片
$connn = new mysqli(‘localhost‘,‘root‘,‘root‘,‘denglu‘);
$sqll = " select * from product where id =x";
$ress = $connn->query($sqll);
$r = $ress->fetch_assoc();
$connn->close();
?>
}
}
以上是关于mysql 强大的trim() 函数的主要内容,如果未能解决你的问题,请参考以下文章