PHP全栈学习笔记17

95 阅读5分钟

image.png

phpmyadmin教程

image.png

管理页进入phpmyadmin

打开C:\wamp\apps\phpmyadmin3.5.1下的配置文件:config.inc

修改密码 创建与修改数据库、数据表

image.png

image.png

image.png

字段类型

Int 整形 Date 时间 Varchar可变长度的字符串,要指定最大位数 Char 固定长度的字符串,指定固定位数 Double Float

索引

Primary,主键
Unique,唯一
Index,索引

统计函数

AVG(字段名) 得出一个表格栏平均值 COUNT(*;字段名) 对数据行数的统计或对某一栏有值的数据行数统计 MAX(字段名) 取得一个表格栏最大的值 MIN(字段名) 取得一个表格栏最小的值 SUM(字段名) 把数据栏的值相加

查询去除重复值:select distinct * from table1

创建数据库

Create DATABASE databasename 

删除数据库

drop database databasename

删除新表

drop table tabname

增加一个列

Alter table tabname add colname coltype

删除一个列

Alter table tabname drop column colname

添加主键

Alter table tabname add primary key(col) 

创建索引

create [unique] index idxname on tabname(col…。) 

创建视图

create view viewname as select statement

指定id,数据传入一个null

image.png

image.png

平均分

image.png

image.png

排序

image.png

image.png

image.png

image.png

image.png

image.png

image.png

mysql教程 连接到一个 MySQL 数据库 mysql_connect() 函数完成

mysql_connect(servername,username,password);

连接数据库

<?php 
&emsp;$con = mysql_connect("localhost",“root",“12345");
&emsp; if (!$con) { 
&emsp;&emsp;&emsp;die('Could not connect: ' . mysql_error()); 
&emsp;} 
&emsp;// some code 
?> 

关闭连接

<?php
  $con = mysql_connect("localhost","peter","abc123"); 
  if (!$con) { die('Could not connect: ' . mysql_error()); } 
   // some code
    mysql_close($con);
 ?> 

创建数据库和创建表

CREATE DATABASE database_name 
CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, column_name3 data_type, ....... ) 

SELECT 语句查询数据

SELECT column_name(s) FROM table_name
<?php 
$con = mysql_connect("localhost","peter","abc123");
 if (!$con) { die('Could not connect: ' . mysql_error()); } 
mysql_select_db(“my_db”, $con); &emsp;//选择数据库
$result = mysql_query("SELECT * FROM person");
 while($row = mysql_fetch_array($result)) {
 echo $row['FirstName'] . " " . $row['LastName'];
 echo "<br />";
 } 
mysql_close($con); 
?> 

mysql_fetch_array

while($row = mysql_fetch_array($result)) {
 echo $row['FirstName'] . " " . $row['LastName'];
 echo "<br />";
 }

mysql_fetch_array() 函数以数组的形式从记录集返回第一行 while loop 语句会循环记录集中的所有记录

连接:

$con = mysql_connect("localhost:3306","root","");

字符集:

mysql_set_charset("gbk",$con);

数据库:

mysql_select_db("ddd",$con);

查询:

$query = "select * from student";
$result = mysql_query($query);

取得结果集:

while($row =mysql_fetch_array($result))

ORDER BY

SELECT column_name(s) FROM table_name ORDER BY column_name 
<?php 
$con = mysql_connect(“localhost”,“peter”,“abc123”);&emsp;//连接数据库
if (!$con) { 
	die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db(“my_db”, $con); &emsp;//选择要操作的数据库名
mysql_query("INSERT INTO person (FirstName, LastName, Age) VALUES ('Peter', 'Griffin', '35')"); 
mysql_query("INSERT INTO person (FirstName, LastName, Age) VALUES ('Glenn', 'Quagmire', '33')"); 
mysql_close($con); 
?> 
<html> <body> 
<form action="insert.php" method="post">
 Firstname: <input type="text" name="firstname" /> 
Lastname: <input type="text" name="lastname" />
 Age: <input type="text" name="age" /> 
<input type="submit" /> 
</form> 
</body> </html>
<?php
$conn = mysql_connect ( "localhost:3306", "root", "" );
if (! $conn) {
	echo "不能连接到MYSQL数据库!";
} else {
	// 选择数据库
	$select = mysql_select_db ( "dada", $conn );
	if ($select) {
		// 查询语句
		$sql = "select * from student";
		//查询命令
		$result = mysql_query($sql,$conn);
		$row = mysql_fetch_array($result);
		var_dump($row);
	} else {
		echo "dada" . "不存在!";
	}
}
mysql_close ( $conn );
?>
<?php
//连接服务器
$conn = mysql_connect("localhost:3306",'root','') or die("不能连接服务器".mysql_error());
//设置来自数据库的数据的字符集
//特别注意:mysql_set_charset("utf8",$conn);
mysql_set_charset("gbk",$conn);
//选择数据库
$selectDb = mysql_select_db("dada",$conn);
if(!$selectDb){
	echo "数据库不存在!";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<?php
header('Content-type: text/html; charset=gbk');
require_once 'dbconfig.php';
//写查询语句
$sql = "SELECT * FROM `student` WHERE 1 LIMIT 0, 30 ";
//执行查询,得到记录集,是一个二维表状结构,有行有列
$result = mysql_query($sql,$conn);
//请注意,查询失败与查询到一个空记录集是两回事
if(!$result){
	die("查询失败!");
}
//取出结果集中的一行
$row = mysql_fetch_array($result);
if($row){
	//var_dump($row);
	foreach ($row as $key=>$value){
		echo $key.":".$value."<br/>";
	}
}
?>
</body>
</html>
<?php
// 连接到服务器,返回值resource或者false
$conn = mysql_connect ( "localhost:3306", "root", "" );
if (! $conn) {
	echo "不能连接到MYSQL数据库!" . mysql_error ();
} else {
	// 设置数据库的字符集
	// 特别注意设置utf-8字符集: mysql_set_charset('utf8',$conn);
	mysql_set_charset ( 'gbk', $conn );
	// 选择数据库,返回值true或者false
	$select = mysql_select_db ( "dada", $conn );
	if ($select) {
		// 撰写查询语句
		$sql = "select * from student";
		// 执行查询命令,得到记录集
		// 记录集是是一个多行多列的表格状多
		// 注意:查询无内容,得到一个空记录集,但查询成功
		$result = mysql_query ( $sql, $conn );
		if (! $result) {
			echo "查询失败!!";
		} else {
			// 如果取出内容不为空,一行数据
			// $row = mysql_fetch_array ( $result );
			// if ($row!=null) {
			echo "<center>查询结果</center>";
			echo "<hr/>";
			echo "<table border=1 width=800 align='center'>";
			echo "<tr>";
			echo "<th>记录号</th><th>学号</th><th>姓名</th><th>班级></th><th>生日</th><th>性别</th><th>民族</th>";
			echo "</tr>";
			while ( $row = mysql_fetch_array ( $result ) ) {
				// 访问关联数组,键名是数据库表的字段名
				echo "<tr align='center'>";
				echo "<td>".$row ['id']."</td>";
				echo "<td>".$row ['studentId']."</td>";
				echo "<td>".$row ['name']."</td><td>".$row ['className']."</td><td>".$row ['birthday']."</td><td>".$row ['sex']."</td><td>".$row ['nation']."</td>";
				echo "</tr>";
			}
		}
	} else {
		echo "gdmec" . "不存在!";
	}
}
mysql_close ( $conn );
?>

显示日历

<script type="text/javascript" src="js/Calendar3.js"></script>

onclick="new Calendar().show(this);"
<html>
<head>
<title>Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script type="text/javascript" src="js/Calendar3.js"></script>
</head>

<body>
	<center>
		<b>增加学生信息</b>
	</center>
	<hr>
	<form name="form1" method="post" action="insertStudentdo.php">
		<table width="300" border="0" align="center" cellpadding="2"
			cellspacing="2">
			<tr>
				<td width="150"><div align="right">学号:</div></td>
				<td width="150"><input type="text" name="studentId" maxlength='8'></td>
			</tr>
			<tr>
				<td><div align="right">姓名:</div></td>
				<td><input type="text" name="name"></td>
			</tr>
			<tr>
				<td width="150"><div align="right">班级:</div></td>
				<td width="150"><input type="text" name="className"></td>
			</tr>
			<tr>
				<td width="150"><div align="right">生日:</div></td>
				<td width="150"><input type="text" id="birthday" name="birthday" onclick="new Calendar().show(this);"></td>
			</tr>

			<tr>
				<td width="150"><div align="right">性别:</div></td>
				<td width="150"><input type="radio" name="sex" value='男'>男</input> <input
					type="radio" name="sex" value='女' checked>女</input></td>
			</tr>

			<tr>
				<td width="150"><div align="right">民族:</div></td>
				<td width="150"><select name="nation">
						<option value='汉族'>汉族</option>
						<option value='白族'>白族</option>
						<option value='回族'>回族</option>
				</select></td>
			</tr>

		</table>
		<p align="center">
			<input type="submit" name="Submit" value="Submit"> <input
				type="reset" name="Reset" value="Reset">
		</p>
	</form>
</body>
</html>
<?php
//连接服务器
$conn = mysql_connect("localhost:3306","root","");
mysql_set_charset("gbk",$conn);
mysql_select_db("dada");
//取数据
$studentId = $_REQUEST['studentId'];
$name = $_REQUEST['name'];
$className = $_REQUEST['className'];
$birthday = $_REQUEST['birthday'];
$sex = $_REQUEST['sex'];
$nation = $_REQUEST['nation'];
//写语句
$sql= "INSERT INTO `student` VALUES (null,'$studentId','$name','$className','$birthday','$sex','$nation')";
//执行,返回值是true false
$result = mysql_query($sql,$conn);
if(!$result){
	echo "插入失败,语句写错了,请检查!!";
}else{
	echo "数据保存成功!!!<a href='insertStudent.php'>返回</a>";
}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<script type="text/javascript" src="js/Calendar3.js"></script>
<title>查询</title>
</head>
<body>
<?php
session_start();
if(!isset($_SESSION["userName"])){
	header("location:login.php");
}

// 连接服务器
$conn = mysql_connect ( "localhost:3306", "root", "" ) or die ( "不能连接到服务器" . mysql_error () );
// 设定字符集
mysql_set_charset ( "gbk", $conn );
// 选择数据库
$sel = mysql_select_db ( "dada" );
if ($sel) {
	// 生成查询语句
	$sql = "SELECT * FROM `student` LIMIT 0, 30 ";
	// 查询结果是一个二维(多行多列)的表状结构
	// 如果查询结果为空,注意不是查询失败,只不过$result是空记录集
	$result = mysql_query ( $sql, $conn );
	
	echo "<center><b>查询结果</b></center>";
	echo "<hr>";
	echo "<table width='100%' border=1 cellpadding=2 >";
	echo "<tr><th>编号</th><th>学号</th><th>班级</th><th>生日</th><th>性别</th><th>民族</th><th>操作</th></tr>";
	// 取出一行,MYSQL_ASSOC是一个常量,注意不加双引号,表示返回结果是关联数组
	if ($result) {
		echo "<tr>";
		// mysql_fetch_array,取出一行后,指针会自动指向下一行
		while ( $row = mysql_fetch_array ( $result, MYSQL_ASSOC ) ) {
			// 关联数组的键名是表中的字段名
			echo "<td>" . $row ["id"] . "</td><td>" . $row ["studentId"] . "</td><td>" . $row ["name"] . "</td><td>" . $row ["className"] . "</td><td>" . $row ["sex"] . "</td><td>" . $row ["nation"] . "</td><td><a href=deleteStudent.php?id=".$row ["id"].">删除</a></td></tr>";
			// echo $row ["id"] . " " . $row ["name"]."<br/>";
		}
		echo "</table>";
	} else {
		echo "查询失败";
	}
} else {
	echo "数据库不存在!!!";
}
?>
<?php

//连接服务器
$conn = mysql_connect("localhost:3306","root","");
mysql_set_charset("gbk",$conn);
mysql_select_db("dada");
//取数据
$id = $_REQUEST['id'];
//写语句
$sql= "delete from student where id='$id'";
echo $sql;
//执行,返回值是true false
$result = mysql_query($sql,$conn);
if(!$result){
	echo "删除失败,语句写错了,请检查!!";
}else{
	header("location:index.php");
}
?>

结言

好了,欢迎在留言区留言,与大家分享你的经验和心得。

感谢你学习今天的内容,如果你觉得这篇文章对你有帮助的话,也欢迎把它分享给更多的朋友,感谢。

感谢!承蒙关照!您真诚的赞赏是我前进的最大动力!

image

image