PHP全栈学习笔记28

数据库Mysql概述,数据库操作,数据表操作,数据类型,管理数据库

order by asc; 升序
desc 降序

sql标准语言:
数据查询语言 select
数据定义语言 create/alter/drop
数据操作语言 insert delete update
数据控制语言 grant revoke 为用户赋予特权,收回特权

mysql支持的数据类型为主要为3类:
数字类型,字符串类型,日期和时间类型。

数字类型分整数型和浮点型两种:

整数数据类型:

tinyint
bit
bool
smallint
mediumint
int
bigint

浮点数据类型:

float, double, decimal

字符串类型:

char varchar, 
text blob,
set enum,

日期和时间数据类型:

date time datetime timestamp year

php数据库编程
mysql_connect()函数连接mysql服务器

<?php
$conn = mysql_connect("localhost", "root", "root") or die("连接失败".mysql_error());
?>

mysql_select_db()函数选择mysql数据库

<?php
$conn = mysql_connect("localhost","root","root");
$select = mysql_select_db("dashu", $conn);
if($select){
echo "成功";
}
?>

mysql_query()函数
mysql_fetch_array()函数将结果集返回到数组中

mysql_assoc将数组采用关联索引
mysql_num:数组采用数字索引
mysql_both:包含关联和数字索引的数组

mysql_fetch_row()函数从结果集中获取一行作为枚举数组

while($result = mysql_fetch_row($arr)){
$result[1];

mysql_num_rows()函数获取查询结果集中的记录数

insert update delete select
$arr = mysql_query("select * from tb", $conn);
while($result = mysql_fetch_array($arr)){
mysql_num_rows($arr)
mysql_free_result($sql); // 关闭记录集
mysql_close($conn); // 关闭mysql数据库服务器
<a href="xx.php?id=<?php echo $row->id;?>">

$id=$_GET[id];

搜索:

<?php
include_once("conn/conn.php"); // 包含数据库连接文件
if(isset($_POST['sub'])){
if($_POST['text'] == "" || $_POST['text'] == "请输入关键字" ) {
 echo "<script>alert('请输入查询内容');</script>";
 }else{
  $sql = "select * from tb where name like '% ".$_POST['text']." %'";
 $rs = mysql_query($sql, $conn);
 ?>
<?php
while($rst = mysql_fetch_row($rs)){
?>

cookie和session

<?php
setcookie("TMCookie", 'xx.com');
...(, time()+60);

session会话
session设置时间
客户端没有禁止cookie

<?php
$time=1*60;
session_set_cookie_params($time);
session_start();
$_SESSION[username] = "dashu";
?>

不建议使用这个函数

<?php
session_start();
$time = 1*60;
setcookie(session_name(), session_id(), time()+$time, "/");
$_SESSION['user']="da";
?>

session数据库存储

session_set_save_handler()

function _session_open($save_path, $session_name){
 global $handle;
 $handle = mysql_connect('localhost','root','root') or die('连接失败');
 mysql_select_db('db',$handle) or die('失败');
 return (true);
}
}
function _session_close(){
 global $handle;
 mysql_close($handle);
 return(true);
}
<?php
header("content-type:image/png");	//设置页面编码		
$im = imagecreate(65,25);
imagefill($im, 0, 0, imagecolorallocate($im,200, 200, 200));
$validatorCode=$_GET['code'];
imagestring($im,rand(3,5),10,3,substr($validatorCode,0,1),imagecolorallocate($im,0,rand(0,255),rand(0,255)));
imagestring($im,rand(3,5),25,6,substr($validatorCode,1,1),imagecolorallocate($im,rand(0,255),0,rand(0,255)));
imagestring($im,rand(3,5),36,9,substr($validatorCode,2,1),imagecolorallocate($im,rand(0,255),rand(0,255),0));
imagestring($im,rand(3,5),48,12,substr($validatorCode,3,1),imagecolorallocate($im,0,rand(0,255),rand(0,255)));
for($i=0;$i<200;$i++){
    imagesetpixel($im,rand()%70,rand()%30,imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)));
}
imagepng($im);
imgedestory();
?>
原文地址:https://www.cnblogs.com/dashucoding/p/11140258.html