【慕课笔记】PHP与MySQL关系大揭秘

7 篇文章 0 订阅
3 篇文章 0 订阅
3-3
mysql_fetch_row与mysql_fetch_array的区别:
1、mysql_fetch_row取一条数据产生一个索引数组
2、mysql_fetch_array默认状态下取一条数据产生一个索引数组和一个关联数组

$arr=mysql_fetch_row($query);
echo $arr['name'];
/*
mysql_fetch_array的第二个参数
1、MYSQL_ASSOC - 关联数组
2、MYSQL_NUM - 数字数组(即索引数组)
3、MYSQL_BOTH - 默认(混合的数组,既有索引数组又有关联数组)
*/


/********关联数组*********/
mysql_fetch_array($query, MYSQL_ASSOC);

/********索引数组*********/
mysql_fetch_row($query);
mysql_fetch_array($query, MYSQL_NUM);


/********关联+索引数组*********/
mysql_fetch_array($query);
mysql_fetch_array($query, MYSQL_BOTH); 


3-4  mysql_fetch_assoc()以关联数组形式获取数据

mysql_fetch_assoc --获取和显示数据

mysql_fetch_array(‘资源标志符’, MYSQL_ASSOC);

两个都是等效的。


3-7 mysql_fetch_object以对象形式获取数据

$arr=mysql_fetch_obj($query);
print_r($arr);


while($arr=mysql_fetch_obj($query)){
	echo $arr->name;
	echo "<br/>";
}

4-1 mysql_num_rows()获取结果集中行的数目

mysql_num_rows() --结果集中行的数目

echo mysql_num_rows($query);

if($query && mysql_num_rows($query)){
	//进行数据输出
	while($row=mysql_fetch_row($query)){

	}
}else{
	echo "没有数据";

}

4-3 mysql_result() 获取结果集中一个字段的值

$query=mysql_query('select * from fruitshop');
echo mysql_result($query,2,1); //结果集的地址, 行号从0开始, 字段名或者偏移量从0开始(偏移量可以理解为表格中的第几列)

4-4 mysql_affected_rows() 获取前一次操作受影响的记录行数

mysql_affected_rows --受影响的记录行数

返回前一次受Insert, update, delete影响的记录的行数

<?php
header("Content-type: text/html; charset=utf-8");
$con=mysql_connect('localhost','root','123');
mysql_select_db('info');
mysql_query('set names utf8');

//mysql的增删改
//通过mysql_query向mysql数据库传递insert update delete语句


//改
if(mysql_query('update fruitshop set num=5 where id=1')){
	echo "修改成功, 修改的数据条数为";
	echo mysql_affected_rows($con); //连接标志符,当修改的数据和之前一样的时候,影响条数为0
}else{
	echo "修改失败";
}


//增
if(mysql_query('insert into fruitshop(name,num,price) values(\'西红柿\',5,6);')){
	echo "插入成功, 插入的数据条数为";
	echo mysql_affected_rows($con); //只能获取到前一次操作所影响的行数
}else{
	echo "插入失败";
}

?>

5-1 文章发布系统的架构分析和设计

<插入图片:数据库设计>

<插入图片:系统架构设计>





5-2 创建配置文件和初始化文件

文章发布系统 - MYSQL连接、初始化程序

文件编写意义

统一配置、方便管理、减少代码冗余

配置文件编写
config.php

MYSQL初始化程序文件编写
connect.php

config.php

<?php
	header("Content-type: text/html; charset=utf-8");
	define('HOST','127.0.0.1');
	define('USERNAME','root');
	define('PASSWORD','123');
?>

connect.php

<?php
	require_once('config.php');
	//连库
	if(!($con=mysql_connect(HOST, USERNAME, PASSWORD))){
		echo mysql_error();
	}
	
	//选库
	if(!mysql_select_db('info')){
		echo mysql_error();
	}

	//定义字符集
	if(mysql_query('set names utf8')){
		echo mysql_error();
	}

//这些error开发完成后都需要屏蔽,以免爆出数据库漏洞

?>

5-3 发布文章

文章发布系统 - 文章发布

文章发布界面编写
article.add.php

文章发布处理程序的编写
article.add.handle.php

程序测试

article.add.php
html页面
form表格提交

article.add.handle.php

<?php
	require_once('../connect.php');
	//把传递过来的信息入库
	if(!(isset($_POST['title'])&&(!empty($_POST['title'])))){
		echo "<script>alert('标题不能为空!');window.location.href='article.add.php'";</script>";
	}
	print_r($_POST);
	$title=$_POST['title'];
	$author=$_POST['author'];
	$description=$_POST['description'];
	$content=$_POST['content'];
	$dateline=time(); //获取当前unix时间戳
	$insertsql = "insert into article(title,author,description,content,dateline) values ('$title','$author','$description','$content',$dateline);";
	//echo $insertsql;
	if(mysql_query($insertsql)){
		echo "<script>alert('发布文章成功');window.location.href='article.add.php';</script>";
	}else{
		echo "<script>alert('发布失败');window.location.href='article.add.php';</script>";
	}
?>

5-4 修改文章

文章发布系统 - 文章修改

文章修改界面编写
article.modify.php

文章修改处理程序的编写
article.modify.handle.php

程序测试


article.modify.php

<?php

	require_once('../connect.php');
	//读取旧信息
	$id=$_GET['id'];
	$query=mysql_query("select * from article where id=$id");
	$data=mysql_fetch_assoc($query);	
?>
<html>
	<input type="hidden" name="id" id="id" value="<?php echo $data['id'] ?>" />
	<input type="text" name="title" id="title" value="<?php echo $data['title'] ?>" />
	<textarea name="description" id="description" cols="60" rows="5"><?php echo $data['description'] ?></textarea>
</html>


article.modify.handle.php

<?php
	require_once('../connect.php');
	$id=$_POST['id'];
	$title=$_POST['title'];
	$author=$_POST['author'];
	$description=$_POST['description'];
	$content=$_POST['content'];
	$dateline=time(); //获取当前unix时间戳
	$updatesql = "update article set id=$id, title='$title', author='$author', description='$description', content='$content', dateline=$dateline where id=$id";
	//echo $insertsql;
	if(mysql_query($updatesql)){
		echo "<script>alert('修改文章成功');window.location.href='article.manage.php';</script>";
	}else{
		echo "<script>alert('修改文章失败');window.location.href='article.manage.php';</script>";
	}
?>

5-5 删除文章

文章发布系统 - 文章删除

文章删除处理程序的编写
article.del.handle.php

程序测试


article.del.handle.php

<?php
	require_once(../connect.php);
	$id=$_GET['id'];
	$deletesql="delete from article where id=$id";
	if(mysql_query($deletesql)){
		echo "<script>alert('删除文章成功!');</script>";
	}else{
		echo "<script>alert('删除文章失败!');</script>";
	}
?>

5-6 文章管理列表

文章发布系统 - 文章管理列表

文章管理列表编写
article.manage.php

程序测试


article.manage.php

<?php
	require_once('../connect.php');
	$sql="select * from article order by dateline desc";
	$query=mysql_query($sql);
	if($query&&mysql_num_rows($query)){
		while($row=mysql_fetch_assoc($query)){
			$data[]=$row; //二维数组,这里面包含若干个一维数组
		}
	}else{
		$data=array();
	}

?>

输出列表:
<?php
	if(!empty($data)){
		foreach($data as $value){
?>
	<tr>
		<td><?php echo $value['id']?></td>
		<td><?php echo $value['title']?></td>
		<td>
			<a href="article.del.handle.php?id=<?php echo $value['id']?>">删除</a>
			<a href="article.modify.handle.php?id=<?php echo $value['id']?>">修改</a>
		</td>
	</tr>
<?php	
		} //end foreach 
	}//end if
?>

5-7 总结

<插入图片>

<插入图片>


6-1 文章列表页






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值