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");
$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; (删除的是整个表格,暴力删除)
AND 与
OR 或者
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
header("Content-type:text/html;charset=utf-8");
$con = mysql_connect("localhost","root","root");
if($con){
echo "连接成功"."<br>";
mysql_select_db("2012");
}
mysql_query("insert into student values (9,'小明',20)",$con);
mysql_close ($con);
?>
登录验证
<?php
header("content-type:text/html;charset=utf-8");
$name = $_GET['userName'];
$con = mysqli_connect("localhost","root","root","需要连接的库名");
$sql = "select * from sogin where nickname='$name'";
$result = mysqli_query($con,$sql);
$rows = mysqli_num_rows($result);
if($rows==1){
echo "亲,该用户名已存在,换一个试试哦";
}else{
echo "亲,用户名可以使用哟";
}
mysqli_close($con);
?>
注册
<?php
header("content-type:text/html;charset=utf-8");
$name = $_POST['nickname'];
$pass = $_POST['password'];
$con = mysqli_connect("localhost","root","root","需要连接的库名");
$sql = "insert into sogin (nickname,password) values('$name','$pass')";
$result = mysqli_query($con,$sql);
if($result){
echo "注册成功,请登录";
}else{
echo "注册失败,请重新注册";
}
mysqli_close($con);
?>
登录
<?php
header("content-type:text/html;charset=utf-8");
$name = $_POST["nickname"];
$pass = $_POST["password"];
$con = mysqli_connect("localhost","root","root","需要连接的库名");
$sql = "select * from sogin where nickname='$name' and password='$pass'";
$result = mysqli_query($con,$sql);
$rows = mysqli_num_rows($result);
if($rows==1){
echo "恭喜您,登陆成功";
}else{
echo "登录失败,用户名或密码输入不正确";
}
mysqli_close($con);
?>