We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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 属性 = 值;
select count(*) from 表名 where 字段名 = 值;
SELECT min(`字段名`) FROM `表名` WHERE 1
SELECT max(`字段名`) FROM `表名` WHERE 1
SELECT sum(`字段名`) FROM `表名` WHERE 1
SELECT sqrt(`字段名`) FROM `表名` WHERE 1
SELECT first(*) FROM `表名` WHERE 1
SELECT last(*) FROM `表名` WHERE 1
SELECT len(*) FROM `表名` WHERE 1
select now();
select rand(); -- 可以通过乘一个数达到想要的范围 select rand() *100;
select concat('AAA','BBB');
1、select * FROM 表名 where 字段名 >= '2002-01-01' AND 字段名 <= '2004-01-01'; 2、select * from 表名 where 字段名 between '2002-01-01' and '2004-01-01' between--and:在谁和谁之间
like 模糊匹配 %叫通配符。
select * from 表名 where 字段名 like '%王%'
select * from 表名 order by 字段名 ASC; 默认正序排序 ASC(可被省略) select * from 表名 order by 字段名 DESC; 逆序排序 DESC
两表联查,查询两个表,通过where子句把两个表中相同的字段关联起来
select 表1字段名,表2字段名 from 表1,表2 where 表1字段名 = 表2字段名
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"; }
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 "连接成功"; }
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 = " delete from `news` WHERE `newsid`=3 "; $result = mysql_query($sql,$conn);
$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);
The text was updated successfully, but these errors were encountered:
No branches or pull requests
MySQL语句总结
显示数据库
创建数据库并设置字符集
通用字符集:utf8;排序规则:utf8_general_ci
选中数据库
创建数据表
查询语句
插入语句
修改语句
删除语句
count函数---查数量
min函数---求最小值
max函数---求最大值
sum函数---求和
sqrt函数---求平方根
first函数---符合条件的第一个
last函数---符合条件的最后一个
len函数---求长度
now函数---显示当前的时间
select now();
rand函数---得到一个随机数 返回0-1之间的任意一个数字
concat函数---拼接字符串
条件查询where子句
范围查询
筛选查询
like 模糊匹配 %叫通配符。
排序
order by子句
多表联查
两表联查,查询两个表,通过where子句把两个表中相同的字段关联起来
PHP连接mysql的操作
php连接数据库(面向过程连接)
例:
php连接数据库(面向对象连接)
例:
php连接mysql(PDO连接)
例
选择指定的数据库
设置存储编码
执行SQL语句
通过外部输入来插入mysql
关闭连接
例子
The text was updated successfully, but these errors were encountered: