Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP与MySQL操作 #23

Open
hubvue opened this issue Dec 23, 2018 · 0 comments
Open

PHP与MySQL操作 #23

hubvue opened this issue Dec 23, 2018 · 0 comments
Labels
PHP Extra attention is needed

Comments

@hubvue
Copy link
Owner

hubvue commented Dec 23, 2018

MySQL语句总结

显示数据库

shwo databases;

创建数据库并设置字符集

通用字符集:utf8;排序规则:utf8_general_ci

create database if not exists 数据库名 default charset utf8 collate utf8_general_ci;

选中数据库

use database; 

创建数据表

create table `数据库名`,`数据表名``字段名` 类型  是否非空 自动增长  注释 
);

查询语句

查所有    select * from 表名
条件查    select * from 表名 where 字段名 = 值;

插入语句

insert into 表名 values("","","","","");

修改语句

update 表名 set 属性 =where 属性 =

删除语句

delete from 表名 where 属性 = 值; 

count函数---查数量

select count(*) from 表名 where 字段名 = 值;

min函数---求最小值

SELECT min(`字段名`) FROM `表名` WHERE 1 

max函数---求最大值

SELECT max(`字段名`) FROM `表名` WHERE 1 

sum函数---求和

SELECT sum(`字段名`) FROM `表名` WHERE 1 

sqrt函数---求平方根

SELECT sqrt(`字段名`) FROM `表名` WHERE 1 

first函数---符合条件的第一个

SELECT first(*) FROM `表名` WHERE 1 

last函数---符合条件的最后一个

SELECT last(*) FROM `表名` WHERE 1 

len函数---求长度

SELECT len(*) FROM `表名` WHERE 1 

now函数---显示当前的时间

select now();

rand函数---得到一个随机数 返回0-1之间的任意一个数字

select rand();
-- 可以通过乘一个数达到想要的范围
select rand() *100;

concat函数---拼接字符串

select concat('AAA','BBB');

条件查询where子句

范围查询
1select * FROM 表名 where 字段名 >= '2002-01-01' AND 字段名 <= '2004-01-01';
2select * from 表名 where 字段名 between '2002-01-01' and '2004-01-01'
                between--and:在谁和谁之间  
筛选查询

like 模糊匹配 %叫通配符。

select * from 表名 where 字段名 like '%王%'

排序

order by子句
select * from 表名 order by 字段名 ASC;  默认正序排序 ASC(可被省略)
select * from 表名 order by 字段名 DESC;  逆序排序  DESC

多表联查

两表联查,查询两个表,通过where子句把两个表中相同的字段关联起来

select1字段名,表2字段名 from1,表2 where1字段名 =2字段名

PHP连接mysql的操作

php连接数据库(面向过程连接)

mysql_connect(servername,username,password); 
    //servername : 可选,规定要连接的服务器,默认为:“localhost:3306”
    //username : 可选,规定登录使用的用户名。默认值是拥有服务器进程的用户名称。
    //password:可选,规定登录所用的密码,默认是“”
例:
$con = mysql_connect("localhost","root",'123456');
if(!$con) {
    die('Could not connect:');       //die() 函数输出一条消息,并退出当前脚本。
}else {
    echo "mysql connect ok";
}

php连接数据库(面向对象连接)

new mysqli(servername,username,password); 
    //servername : 可选,规定要连接的服务器,默认为:“localhost:3306”
    //username : 可选,规定登录使用的用户名。默认值是拥有服务器进程的用户名称。
    //password:可选,规定登录所用的密码,默认是“”
例:
$servername = "localhost";
$username = "root";
$password = "123456";

//创建连接
$conn = new mysqli($servername,$username,$password);

// 检测连接
if($conn -> connect_error){
    die("连接失败" . $conn->connect_error);
}else {
    echo "连接成功";
}

php连接mysql(PDO连接)

new PDO("mysql:host=$servername;", $username, $password);
    //servername : 可选,规定要连接的服务器,默认为:“localhost:3306”
    //username : 可选,规定登录使用的用户名。默认值是拥有服务器进程的用户名称。
    //password:可选,规定登录所用的密码,默认是“”
$servername = "localhost";
$username = "username";
$password = "password";
try {
    $conn = new PDO("mysql:host=$servername;", $username, $password);
    echo "连接成功"; 
}
catch(PDOException $e)
{
    echo $e->getMessage();
}

选择指定的数据库

mysql_select_db($database,$conn);

设置存储编码

mysql_query("set names 'utf8'");

执行SQL语句

$sql = " delete from `news` WHERE   `newsid`=3 ";
$result = mysql_query($sql,$conn); 

通过外部输入来插入mysql

$newstitle = $_REQUEST['newstitle'];
$newsimg = $_REQUEST['newsimg'];
$newscontent = $_REQUEST['newscontent'];
$addtime = $_REQUEST['addtime'];
$sql = "insert into `news`(`newstitle`,`newsimg`,`newscontent`,`addtime`) values ('$newstitle','$newsimg','$newscontent','$addtime')";
mysql_query($sql,$conn); 

关闭连接

mysql_close($conn);

例子

$servername = "localhost";
$username = "root";
$password = "123456";
$conn = mysql_connect($servername , $username , $password);
if(!$conn) {
    die("Could not Connect" . mysql_error());
}else{
     mysql_select_db("phpadmin",$conn);
     mysql_query("set names 'utf8'");
    $sql = "select * from news";
    $result = mysql_query($sql,$conn);      //返回的是一个数组
    $arr = array();
    while($row = mysql_fetch_array($result)){ //获取数组中的每一行
        array_push($arr,array(
            "newsid"=>$row["newsid"],
            "newstitle"=>$row["newstitle"],
            "newsimg"=>$row["newsimg"],
            "newscontent"=>$row["newscontent"],
            "addtime"=>$row["addtime"]
            ));
    }
    $result = array("errcode"=> 0,"result" => $arr);
    if(!$result) {
        die('Error:'.mysql_error());
    }else {
        echo json_encode($result);
    }
}
mysql_close($conn);
@hubvue hubvue added the PHP Extra attention is needed label Jan 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PHP Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant