当前位置:Gxlcms > PHP教程 > php学习正式起航(6)

php学习正式起航(6)

时间:2021-07-01 10:21:17 帮助过:24人阅读

现在讲讲php的错误处理

  1. <?php
  2. $file=fopen("1.txt","r");
  3. ?>

如果文件不存在 系统会直接报错误
用die就可以自己写错误信息 die是死亡的意思,表示错误

为了避免用户获得类似上面的错误消息,我们在访问文件之前检测该文件是否存在

  1. <?php
  2. if(!file_exists("1.txt"))
  3. {
  4. die("File not found");
  5. }
  6. else
  7. {
  8. $file=fopen("1.txt","r");
  9. }
  10. ?>

如果文件不存在,会打印File not found,而不是系统冒出的错误


其实php的错误处理远不止这些,后面还有异常等
放在以后在说吧

现在开始重头戏,终于要用php对mysql数据库进行操作了

首先是连接数据库

  1. <?php
  2. $link = mysql_connect('localhost','root','');
  3. if (!$link) {
  4. die('Could not connect to MySQL: ' . mysql_error());
  5. }
  6. echo 'Connection OK'; mysql_close($link);
  7. ?>

$link 这个又是一个资源变量,表示数据库连接状态,你可以试着打印看看会出现什么
mysql_connect函数就是连接mysql数据库啦
有3个函数 分别是服务器地址 数据库用户名 数据库用户密码
mysql_error函数可以知道连接错误原因
mysql_close是关闭数据库连接

现在先创建一个数据库
create database test;
然后创建一个表

470.jpg

超级简单吧

进入phpmyadmin看看

471.jpg

472.jpg

首先我们插入几条数据看看

现在会连接mysql数据库了,接着我们要在mysql其中一个数据库里插入数据了

  1. <?php
  2. $link = mysql_connect('localhost','root','');
  3. if (!$link) {
  4. die('Could not connect to MySQL: ' . mysql_error());
  5. }
  6. mysql_select_db('test',$link);
  7. mysql_query("insert into user (name,age) values ('harry',15)",$link);
  8. mysql_close($link);
  9. ?>

mysql_select_db就是选择数据库的函数
mysql_query就是执行sql语句的函数,任何sql都可执行,包括创建数据库和表
两个函数的第2个参数可以省略
mysql_select_db('test');
mysql_query("insert into user (name,age) values ('harry',15)");
如果第2个参数没有指定连接标识符,则使用上一个打开的连接。如果没有打开的连接,将无参数调用 mysql_connect() 来尝试打开一个并使用之

现在看看是否能插入成功

473.jpg

ok 已经插入了

现在我们用表单形式插入数据

  1. <html>
  2. <body> <form action="" method="post">
  3. Name: <input type="text" name="name" />
  4. Age: <input type="text" name="age" />
  5. <input type="submit" />
  6. </form> </body>
  7. </html>
  1. <?php
  2. $name=$_POST['name'];
  3. $age=$_POST['age'];
  4. if(isset($name)&&isset($age))
  5. {
  6. $link = mysql_connect('localhost','root','');
  7. if (!$link) {
  8. die('Could not connect to MySQL: ' . mysql_error());
  9. }
  10. mysql_select_db('test');
  11. $sql="insert into user (name,age) values ('$name',$age)";
  12. if(mysql_query($sql))
  13. echo "Insert Success!";
  14. else
  15. echo "Insert Fail!".mysql_error();
  16. mysql_close($link);
  17. }
  18. ?>

mysql_query() 在执行sql语句成功时返回 TRUE,出错时返回 FALSE

现在来显示数据吧

  1. <html>
  2. <body> <form action="" method="post">
  3. Name: <input type="text" name="name" />
  4. Age: <input type="text" name="age" />
  5. <input type="submit" />
  6. </form>
  7. <?php
  8. //数据库连接
  9. $link = mysql_connect('localhost','root','');
  10. if (!$link) {
  11. die('Could not connect to MySQL: ' . mysql_error());
  12. }
  13. mysql_select_db('test'); //插入
  14. $name=$_POST['name'];
  15. $age=$_POST['age'];
  16. if(isset($name)&&isset($age))
  17. { $sql="insert into user (name,age) values ('$name',$age)";
  18. if(mysql_query($sql))
  19. echo "Insert Success!";
  20. else
  21. echo "Insert Fail!".mysql_error(); }
  22. //查询
  23. $sql="select * from user";
  24. $result=mysql_query($sql);
  25. while($row = mysql_fetch_array($result))
  26. {
  27. echo "<table>";
  28. echo "<tr>";
  29. echo "<td>" . $row['id'] . "</td>";
  30. echo "<td>" . $row['name'] . "</td>";
  31. echo "<td>" . $row['age'] . "</td>";
  32. echo "</tr>";
  33. echo "</table>";
  34. } mysql_free_result($result);
  35. mysql_close($link); ?>
  36. </body>
  37. </html>


$result=mysql_query($sql); $result也是一个资源变量,当mysql_query执行查询sql语句的时候,其返回结果就不再是true和false ,而是个结果集,你可以想象就是mysql_query查询的结果返回一张表给了$result,$result就是一张表(结果集)

mysql_fetch_array函数则是专门处理结果集的,从结果集中取得一行作为关联数组,或数字数组,或二者兼有,mysql_query($sql); 其本身就是结果集了
为什么要循环呢,因为mysql_fetch_array一次只能在结果集(表)取一行数据
然后把这一行数据以一维关联数组的形式给$row,$row就是一维关联数组
mysql_fetch_array有个指针指着每一行,每当执行完这个函数的时候,指针就自动指向下一行,如果当函数再次被执行,就能获取到这行的数据,直到最后一行,指针就会指向空的,所以循环也会因为空而结束
不要以为$row是二维数组,它一直是一维,而且每循环一次就被重新赋值一次
有人可能怀疑,数组也能做为循环条件?可以的
$a=array(1,2)
while($a)
这样是能够循环的,只不过是死循环
因为while括号里只要不为0的东东都能循环


$row['name'] 就个就是从数组里取值了,关联数组还记得吧
其实用数组下标也可以的,之前没说,其实关联数组也具有普通数组的特性,且更强大
所以这是可以的

  1. echo "<td>" . $row[0] . "</td>";
  2. echo "<td>" . $row[1] . "</td>";
  3. echo "<td>" . $row[2] . "</td>";

mysql_free_result是释放资源
不是必须使用,仅需要在考虑到返回很大的结果集时会占用多少内存时调用。在脚本结束后所有关联的内存都会被自动释放的


另外mysql_fetch_row也可以查询结果,不过跟mysql_fetch_array比,弱爆了,这里只是介绍下曾经有这么个东西。。。

//查询

  1. $sql="select * from user";
  2. $result=mysql_query($sql);
  3. while($row = mysql_fetch_row($result))
  4. {
  5. echo "<table>";
  6. echo "<tr>";
  7. echo "<td>" . $row[0] . "</td>";
  8. echo "<td>" . $row[1] . "</td>";
  9. echo "<td>" . $row[2] . "</td>";
  10. echo "</tr>";
  11. echo "</table>";
  12. }

mysql_fetch_row() 从和指定的结果标识关联的结果集中取得一行数据并作为数组返回。每个结果的列储存在一个数组的单元中
则row不再是关联数组而是普通数组,所以只能用数组下标


下面说说几个常用的数据显示函数
int mysql_num_rows ( resource $result )
mysql_num_rows() 返回结果集中行的数目。此命令仅对 SELECT 语句有效
int mysql_num_fields ( resource $result )
mysql_num_fields() 返回结果集中字段的数目
int mysql_insert_id ([ resource $link_identifier ] )
mysql_insert_id() 返回给定的 link_identifier 中上一步 INSERT 查询中产生的 AUTO_INCREMENT 的 ID 号

重新写下

  1. <?php
  2. header("Content-Type:text/html;charset=gbk");
  3. error_reporting(0);
  4. ?> <html>
  5. <body> <form action="" method="post">
  6. Name: <input type="text" name="name" />
  7. Age: <input type="text" name="age" />
  8. <input type="submit" />
  9. </form> <?php
  10. //数据库连接
  11. $link = mysql_connect('localhost','root','');
  12. if (!$link) {
  13. die('Could not connect to MySQL: ' . mysql_error());
  14. }
  15. mysql_select_db('test'); //插入
  16. $name=$_POST['name'];
  17. $age=$_POST['age'];
  18. if(isset($name)&&isset($age))
  19. { $sql="insert into user (name,age) values ('$name',$age)";
  20. if(mysql_query($sql))
  21. echo "Insert Success!";
  22. else
  23. echo "Insert Fail!".mysql_error(); } //查询
  24. $sql="select * from user";
  25. $result=mysql_query($sql);
  26. while($row = mysql_fetch_row($result))
  27. {
  28. echo "<table>";
  29. echo "<tr>";
  30. echo "<td>" . $row[0] . "</td>";
  31. echo "<td>" . $row[1] . "</td>";
  32. echo "<td>" . $row[2] . "</td>";
  33. echo "</tr>";
  34. echo "</table>";
  35. }
  36. echo "总共".mysql_num_rows($result)."记录<br/>";
  37. echo "每行".mysql_num_fields($result)."字段";
  38. mysql_free_result($result);
  39. mysql_close($link);
  40. ?>
  41. </body>
  42. </html>

header("Content-Type:text/html;charset=gbk"); 这个是表明文件编码格式,显示中文需要这样
error_reporting(0); 屏蔽一切系统提示的注意,警告,和错误

现在完成 修改和删除部分

  1. <?php
  2. header("Content-Type:text/html;charset=gbk");
  3. error_reporting(0);
  4. ?> <html>
  5. <body> <form action="" method="post">
  6. Name: <input type="text" name="name" />
  7. Age: <input type="text" name="age" />
  8. <input type="submit" />
  9. </form> <?php
  10. //数据库连接
  11. $link = mysql_connect('localhost','root','');
  12. if (!$link) {
  13. die('Could not connect to MySQL: ' . mysql_error());
  14. }
  15. mysql_select_db('test');
  16. //插入
  17. $name=$_POST['name'];
  18. $age=$_POST['age'];
  19. if(isset($name)&&isset($age))
  20. { $sql="insert into user (name,age) values ('$name',$age)";
  21. if(mysql_query($sql))
  22. echo "Insert Success!";
  23. else
  24. echo "Insert Fail!".mysql_error(); } /
  25. /查询
  26. $sql="select * from user";
  27. $result=mysql_query($sql);
  28. while($row = mysql_fetch_array($result))
  29. {
  30. ?>
  31. <form action="" method="post">
  32. ID: <?=$row['id']?>
  33. Name: <input type="text" name="_name" value="<?=$row['name']?>"/>
  34. Age: <input type="text" name="_age" value="<?=$row['age']?>" />
  35. <input type="hidden" name="id" value="<?=$row['id']?>" />
  36. <input type="submit" value="修改"/>
  37. <a href="index.php?uid=<?=$row['id']?>">删除</a>
  38. </form> <?php
  39. } echo "总共".mysql_num_rows($result)."记录<br/>";
  40. echo "每行".mysql_num_fields($result)."字段";
  41. //修改
  42. $name=$_POST['_name'];
  43. $age=$_POST['_age'];
  44. $id=$_POST['id'];
  45. if(isset($name)&&isset($age)&&isset($id))
  46. { $sql="update user set name='$name',age='$age' where id=$id";
  47. if(mysql_query($sql))
  48. header("location:index.php");
  49. else
  50. echo "Update Fail!".mysql_error(); }
  51. //删除
  52. $uid=$_GET['uid'];
  53. if(isset($uid))
  54. { $sql="delete from user where id=$uid";
  55. if(mysql_query($sql))
  56. header("location:index.php");
  57. else
  58. echo "Delete Fail!".mysql_error(); }
  59. mysql_close($link); ?>
  60. </body>
  61. </html>


至此,php对mysql数据库的增删改查操作就全在这一个页面了,灰常的简单

加了个简单的分页,超简单的。。。。暂时就不讲解怎么个来的了,加了简单的注释,大家应该能看懂代码

  1. <?php
  2. header("Content-Type:text/html;charset=gbk");
  3. error_reporting(0);
  4. ?> <html>
  5. <body> <form action="" method="post">
  6. Name: <input type="text" name="name" />
  7. Age: <input type="text" name="age" />
  8. <input type="submit" />
  9. </form> <?php
  10. //数据库连接
  11. $link = mysql_connect('localhost','root','');
  12. if (!$link) {
  13. die('Could not connect to MySQL: ' . mysql_error());
  14. }
  15. mysql_select_db('test'); //插入
  16. $name=$_POST['name'];
  17. $age=$_POST['age'];
  18. if(isset($name)&&isset($age))
  19. { $sql="insert into user (name,age) values ('$name',$age)";
  20. if(mysql_query($sql))
  21. echo "Insert Success!";
  22. else
  23. echo "Insert Fail!".mysql_error(); } //分页查询
  24. if(isset($_GET['pager']))
  25. $pager=($_GET['pager']-1)*5;
  26. else
  27. $pager=0; $sql="select * from user";
  28. $result=mysql_query($sql);
  29. $num=mysql_num_rows($result); //总共记录数
  30. $page=ceil($num/5); //总共多少页 这里每页5条记录 ceil函数四舍五入的。。
  31. for($i=1;$i<=$page;$i++)
  32. echo "<a href='index.php?pager=".$i."'>".$i."</a>"; $
  33. sql="select * from user limit $pager,5";
  34. $result=mysql_query($sql);
  35. while($row = mysql_fetch_array($result))
  36. {
  37. ?>
  38. <form action="" method="post">
  39. ID: <?=$row['id']?>
  40. Name: <input type="text" name="_name" value="<?=$row['name']?>"/>
  41. Age: <input type="text" name="_age" value="<?=$row['age']?>" />
  42. <input type="hidden" name="id" value="<?=$row['id']?>" />
  43. <input type="submit" value="修改"/>
  44. <a href="index.php?uid=<?=$row['id']?>">删除</a>
  45. </form> <?php
  46. } echo "总共".$num."记录<br/>";
  47. echo "每行".mysql_num_fields($result)."字段"; //修改
  48. $name=$_POST['_name'];
  49. $age=$_POST['_age'];
  50. $id=$_POST['id'];
  51. if(isset($name)&&isset($age)&&isset($id))
  52. { $sql="update user set name='$name',age='$age' where id=$id";
  53. if(mysql_query($sql))
  54. header("location:index.php");
  55. else
  56. echo "Update Fail!".mysql_error(); }
  57. //删除
  58. $uid=$_GET['uid'];
  59. if(isset($uid))
  60. { $sql="delete from user where id=$uid";
  61. if(mysql_query($sql))
  62. header("location:index.php");
  63. else
  64. echo "Delete Fail!".mysql_error(); }
  65. mysql_close($link); ?>
  66. </body>
  67. </html>

暂时先到这里了
php后面内容还有很多,还有对象等知识,光数据操作就还有面向对象的

以上就是php学习正式起航(6)的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!

人气教程排行