PHP与数据库相关的知识点

PHP的简单了解
文件扩展名  php
代码写在<?php   与  ?>之间
<?php
        echo "hello,world";
?>
PHP代码每句话以分号结束
PHP的注释:
单行用  //  或 #  /*	内容	*/ 
环形地址:本机IP
http://localhost/testPhp/HelloWorld.php
http://127.0.0.1/testPhp/HelloWorld.php
编码格式
	中文问题:header("Content-type:text/html;charset=utf-8");
PHP基本语法语法
	php中,定义一个变量要以$符号打头,变量名区分大小写
	php 的变量的数据类型,是变化的,php变量的数据类型是由运行时的上下文决定。
	$age = 250;    $a=90; //相当于我们定义了一个变量 $a,并赋值90    $a="hello"; //表示$a 的类型变化
	数据类型:字符串的连接用  .    echo "Hello World" . "小明";
PHP的流程控制
	顺序控制(从上到下,从左到右)
	分支控制
<form action="4getDate.php" method="post">
		 	<!--  name代表前后端的约定,实现数据前后端的绑定 -->
			姓名:<input type="text" name="userName"/><br>
			密码:<input type="text" name="userPwd"/><br>
			<input type="submit" value="提交"/>
		 </form> 

action:发送数据的地址
method:发送数据的方式  不写默认是 get
get:五菱宏光      效率高    安全性低	运输量大
post:武装押运     效率低    安全性高	运输量小

<?php
    header("Content-type:text/html;charset=utf-8");
//PHP接收前端的数据的方式:
//    $name = $_GET["username"];获取username
//    $pwd = $_GET["pwd"];获取password

//  $name = $_POST["username"];
//  $pwd = $_POST["pwd"];    

   	$name = $_REQUEST["username"];
    $pwd = $_REQUEST["pwd"];    
    
    echo $name.":".$pwd;
?>

数据库的基本操作

创建表
create table 表名(字段1,字段2....,字段N);

create table student
(
      stu_id int,
      stu_name varchar(10) primary key,
      stu_age int
)

---增加一条语句
insert into 表名(字段1,字段2....,字段N);
values(1,值2....,值N);

---方法一
insert into student (stu_id,stu_name,stu_age)
values(123888,"小子",23);

---方法二
insert into 表名
values(1,值2....,值N);

insert into student 
values(2,"点",20);

insert into student 
values(3,"小明",19);

insert into student 
values(6,"小小",21);

insert into student 
values(5,"小华",22);

---删
delete from 表名; 或者  drop table 表名;

delete from student;   (删除的是表里的所有内容,创建的表格还在)

drop table student;    (删除的是整个表格,暴力删除)


ANDOR   或者
where 条件语句

delete from student
where stu_id = 6;    (删除的是表格里id等于3的语句)

delete from student
where stu_id = 3
AND stu_name = "小明";  (删除的是表格里id等于3和名字是小明的语句)

数据库字符的大小不敏感

delete from student
where stu_id = 1
OR stu_id = 2;  (删除的是表格里id等于1或者id等于2的语句)

---改
update 表名 set 字段1=1,字段2=2....,字段N=值N;

update student set stu_age = 66;  (把表格里所有stu_age的数据都改为 66)

update student set stu_age = 20
where stu_id = 2;   (把表格里id等于2的stu_age下的数据改成 20)

---查
select 字段1,....,字段N from 表名
select stu_id,stu_name from student;  (从表格中查找  stu_id,stu_name的数据)

select * from student;  (从表格中查找表格里所有的内容)

select * from student
where stu_id = 2;  (从表格中查找stu_id = 2的所有内容)

PHP连接数据库的步骤和增删查改

<?php
	//后端给前端响应的内容的类型是text/html;字符集是utf-8
	header("Content-type:text/html;charset=utf-8");
	
	//1.连接数据库
	$con = mysql_connect("localhost","root","root");
	
	if($con){
		echo "连接成功"."<br>";
		//2.选择数据库
		mysql_select_db("2012");
	}
	
	// 3.操作:增删查改  mysql_query(sql语句,连接对象);
	// 增
	mysql_query("insert into student values (9,'小明',20)",$con);
	
	// 删
	// mysql_query("delete from student where stu_id = 5",$con);
	
	// 改
	// mysql_query("update student set stu_age = 10 where stu_id = 5",$con);
	
	// 查
	// 返回查询的结果:结果集
	// $result = mysql_query("select * from student",$con);
	// mysql_num_rows(结果集):返回当前结果集的记录数
	// echo mysql_num_rows($result);  //输出数据库里的个数

	// a.关注结果集有几条记录,常用于登录与注册的验证
	// $result = mysql_query("select * from student where stu_id = 2",$con);
	// if(myspl_num_rows($result) == 1){
	// 	echo "登陆成功";
	// }else{
	// 	echo "登陆失败";
	// }
	
	// b.提取数据库中的记录
	// 它获取游标所指的纪录,且每次执行完,向下自动移一行
	// mysql_fetch_assoc(结果集):返回当前结果集游标所指向的对象,且游标自动下移
	// $result = mysql_query("select * from student where stu_id = 2",$con);
	// while($obj = mysql_fetch_assoc($result)){
	// 	echo $obj["stu_id"]." ".$obj["stu_name"]." ".$obj["stu_age"]."<br>";
	// }
	
	// 4.关闭数据库
	mysql_close ($con);
?>

登录验证

<?php
	header("content-type:text/html;charset=utf-8");
	//1、接收前端传来的数据
	$name = $_GET['userName']; //前后端约定好的名
	// 2、连接数据库,查询用户名是否存在
	// 1)、连接数据库
	$con = mysqli_connect("localhost","root","root","需要连接的库名");
	// 2) 执行select语句
    $sql = "select * from sogin where nickname='$name'";
	//登录验证是查询数据库里是否存用户名,给数据库里边的nickname设置主键(primary key)以此来判断用户名是否存在
	$result = mysqli_query($con,$sql);  
    $rows = mysqli_num_rows($result);

   // 3、响应
   if($rows==1){
        echo "亲,该用户名已存在,换一个试试哦";
    }else{
    	echo "亲,用户名可以使用哟";
    }

	// 4.关闭数据库
	mysqli_close($con);
?>

注册

<?php

	header("content-type:text/html;charset=utf-8");
	//1、接收前端传来的数据
	$name = $_POST['nickname'];
	$pass = $_POST['password'];
	// 2、处理(数据库操作)
	// 1).连接数据库
	$con = mysqli_connect("localhost","root","root","需要连接的库名");
	// 2).执行SQL语句
	$sql = "insert into sogin (nickname,password) values('$name','$pass')";
	$result = mysqli_query($con,$sql);
	
	// 3、响应
	if($result){
		echo "注册成功,请登录";
	}else{
		echo "注册失败,请重新注册";
	}

	// 4.关闭数据库
	 mysqli_close($con);
?>

登录

<?php
	
	header("content-type:text/html;charset=utf-8");
	// 1.接收前端传来的数据
	$name = $_POST["nickname"];
	$pass = $_POST["password"];
	
	// 2.连接数据库,查询用户名是否存在
	// 1) 连接数据库
	$con = mysqli_connect("localhost","root","root","需要连接的库名");
	// 2) 执行select语句
	    $sql = "select * from sogin where nickname='$name' and password='$pass'";
		$result = mysqli_query($con,$sql);  
		
		// 3.响应
	    $rows = mysqli_num_rows($result);
	    if($rows==1){
	        echo "恭喜您,登陆成功";
	    }else{
	        echo "登录失败,用户名或密码输入不正确";
	    }
	
	// 4.关闭数据库
		mysqli_close($con);
		
?>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值