Mysql数据库连接、查询、记录集操作代码
Mysql数据库链接代码
1 2 3 4 5 6 7 8 9 10 11 12 | function dbConnect( $hostname , $username , $pass , $db_name , $pconnect =0) { $func = empty ( $pconnect ) ? 'mysql_connect' : 'mysql_pconnect' ; if (! $connect ){ $connect =@ $func ( $hostname , $username , $pass ) or die ( "<font size='2'>Mysql_Error : " .mysql_error(). "<br>Mysql Error Num : " .mysql_errno(). "</font>" ); } @mysql_select_db( $db_name , $connect ) or die ( "<font size='2'> Mysql_Error : " .mysql_error(). "<br>Mysql Error Num : " .mysql_errno(). "</font>" ); return $connect ; } |
注释:
参 数$hostname,$username,$pass,$db_name分别代表Mysql数据库服务器地址,用户名,密码,以及连接的数据库名,通常 情况下hostname一般都是localhost或者127.0.0.1。参数$pconnect默认为0,表示通常情况下是以 mysql_connect函数连接Mysql数据库。
知识点:
mysql_connect与mysql_pconnect的区别: 当执行完当前PHP程序后,PHP自动关闭mysql_connect建立的数据库连接,而mysql_pconnect返回一个持久稳固的数据库连接, 在一定时间内有下一个连接请求时可以被复用,节省了反复连接Mysql数据库的时间,使得访问速度加快,其适用于并发访问量不大的情况,如并发访问量比较 大,则可能会因为Mysql已达到最大连接数, 使之后的请求得不到满足。
mysql_error函数:返回上一个Mysql操作产生的文本错误信息。mysql_errno函数返回上一个Mysql操作中的错误号码,如果没有出错则返回0。
Mysql数据库查询代码
1 2 3 4 5 6 7 | function query_error( $query ) { global $connect ; $temp_bar = "<br>=============================================================================<br>" ; $result = mysql_query( $query , $connect ) or die ( "DB ERROR <br>" . $temp_bar . "<font size='2'> Mysql_Query : " . $query . "<br> Mysql_Error : " .mysql_error(). "<br> Mysql Error Num : " .mysql_errno(). "</font>" . $temp_bar ); return $result ; } |
注释:此函数为Mysql数据库查询函数,等于同mysql_query函数的功能,如果出错则输出出错信息(SQL语句),其实为了防止暴露网站数据库的结构,正式商用时,最好不要输出SQL执行语句。
Mysql记录集操作函数代码(mysql_fetch_array)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function fetch_array( $result , $result_type = MYSQL_ASSOC, $records = "one" ) { if ( $records == "one" ) { return @mysql_fetch_array( $result , $result_type ); } else { for ( $i =0;num_rows( $result ); $i ++) { $info [ $i ] = @mysql_fetch_array( $result , $result_type ); } free_result( $result ); return $info ; } } |
注释:此函数的功能由mysql_fetch_array函数延生而来,在此基础上我增加了对Mysql数据库记录集的读取功能,并以数组形式返回获取的值。
知识点:
mysql_fetch_array函数是mysql_fetch_row函数的扩展版本。第二个参数 result_type有三个值:MYSQL_ASSOC,MYSQL_NUM 和 MYSQL_BOTH。默认值是 MYSQL_BOTH。MYSQL_BOTH:得到一个同时包含关联和数字索引的数组。MYSQL_ASSOC:只得到关联索引(如同mysql_fetch_assoc()那样),MYSQL_NUM :得到数字索引(如同 mysql_fetch_row()那样)。
报错信息函数代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | function error_msg( $msg , $url = "" ) { global $connect ; if ( $connect ) { mysql_close( $connect ); } switch ( $url ) { case "" : $url = "history.go(-1)" ; break ; case "close" : $url = "window.close()" ; break ; default : $url = "document.location.href = '$url'" ; break ; } if (! empty ( $msg )) { echo "<script language='javascript'>alert('$str');$url;</script>" ; } else { echo "<script language='javascript'>$url;</script>" ; } exit ; } |
注释:此函数的功能主要以alert的形式报错并进行页面跳转,是一个通用函数,报错或跳转之前其会先将Mysql数据库连接关闭,用到了mysql_close函数。
调用说明:
从上述Mysql数据库操作的函数代码中,我们可以看到$connect变量是一个全局变量,首先将上述几个函数放入一个文件,如mysqlconnect.php中,然后在声明相关变量并赋值,在dbConnect函数声明后调用此Mysql数据库连接函数,即:
1 2 3 4 5 6 | $hostname = "mysqlserveraddr" ; $username = "yourusername" ; $pass = "youruserpass" ; $db_name = "yourdatabase" ; $connect = dbConnect( $hostname , $username , $pass , $db_name ); |
总结:
通过上面几个Mysql数据库连接、数据库查询、数据库记录集操作函数代码的讲解,在PHP网站开发中Mysql数据库操作的基本函数已包括,根据需要可在此代码基础上改成Mysql数据库类或者利用PHP添加其他的Mysql数据库操作函数都是可行的
练习建一个简单的管理页面
1.管理页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <html xmlns= "http://www.w3.org/1999/xhtml" > <head> <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <table style= "caption-side:top; width:90%; border:0px; font:normal, Georgia, 'Times New Roman', Times, serif; size:auto" cellpadding= "0" cellspacing= "1" bgcolor= "#FF0000" > <caption align= "center" style= "size:+4" >管理页面</caption> <tr bgcolor= "#FFFFFF" > <td>民族代号</td> <td>民族</td> <td>操作</td> </tr> <?php $connect =@mysql_connect( "localhost" , "root" , "" ); mysql_select_db( "1" , $connect ); $a = "select * from nation" ; $result =mysql_query( $a ); while ( $arr =mysql_fetch_row( $result )) { echo "<tr bgcolor= '#FFFFFF' > <td>{ $arr [0]}</td> <td>{ $arr [1]}</td> <td><a onclick=\" return confirm( '确定删除' )\" href= 'shanchu.php?code={$arr[0]}' >删除</a>  <a href= 'xiugai.php?code={$arr[0]}' >修改</a></td> </tr> "; } ?> </table> <a href= "tianjia.php" ><input type= "submit" value= "添加" /></a> <a href= "beifen.php" ><input type= "submit" value= "备份" /></a> </body> </html> |
2 添加页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <html xmlns= "http://www.w3.org/1999/xhtml" > <head> <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <form action= "tianjia1.php" method= "post" > <div>民族代号<input type= "text" name= "code" /></div> <div>民族名称<input type= "text" name= "name" /></div> <input type= "submit" value= "确认" /> </form> </body> </html> |
2.1添加处理页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php $a = $_POST [ "code" ]; $b = $_POST [ "name" ]; $connect =@mysql_connect( "localhost" , "root" , "" ); mysql_select_db( "1" , $connect ); $sql = "insert into nation value ('$a','$b') " ; $result =mysql_query( $sql ); if ( $result ) { header( "location:xianshi.php" ); } else { echo "添加失败" ; } |
3.删除处理页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php $code = $_GET [ "code" ]; $connect =@mysql_connect( "localhost" , "root" , "" ); mysql_select_db( "1" , $connect ); $sql = "delete from nation where code='$code'" ; $result =mysql_query( $sql ); if ( $result ) { header( "location:xianshi.php" ); } else { echo "添加失败" ; } ?> |
4备份处理页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <?php // 备份数据库 $host = "localhost" ; $user = "root" ; //数据库账号 $password = "" ; //数据库密码 $dbname = "1" ; //数据库名称 // 这里的账号、密码、名称都是从页面传过来的 if (@!mysql_connect( $host , $user , $password )) // 连接mysql数据库 { echo '数据库连接失败,请核对后再试' ; exit ; } if (@!mysql_select_db( $dbname )) // 是否存在该数据库 { echo '不存在数据库:' . $dbname . ',请核对后再试' ; exit ; } mysql_query( "set names 'utf8'" ); $mysql = "set charset utf8;\r\n" ; $q1 = mysql_query( "show tables" ); while ( $t = mysql_fetch_array( $q1 )) { $table = $t [0]; $q2 = mysql_query( "show create table `$table`" ); $sql = mysql_fetch_array( $q2 ); $mysql .= $sql [ 'Create Table' ] . ";\r\n" ; $q3 = mysql_query( "select * from `$table`" ); while ( $data = mysql_fetch_assoc( $q3 )) { $keys = array_keys ( $data ); $keys = array_map ( 'addslashes' , $keys ); $keys = join( '`,`' , $keys ); $keys = "`" . $keys . "`" ; $vals = array_values ( $data ); $vals = array_map ( 'addslashes' , $vals ); $vals = join( "','" , $vals ); $vals = "'" . $vals . "'" ; $mysql .= "insert into `$table`($keys) values($vals);\r\n" ; } } $filename = $dbname . date ( 'Ymjgi' ) . ".sql" ; //存放路径,默认存放到项目最外层 $fp = fopen ( $filename , 'w' ); fputs ( $fp , $mysql ); fclose( $fp ); echo "备份成功 <a href=\"xianshi.php\"><input type=\"button\" value=\"返回\" /></a> "; ?> |
4修改页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php $a = $_GET [ "code" ]; //获取数据 $connect =@mysql_connect( "localhost" , "root" , "" ); //造链接 mysql_select_db( "1" , $connect ); //选择操作的数据库 $sql = "select * from nation where code=\"{$a}\" " ; //数据库需要执行的操作 $result =mysql_query( $sql ); //执行操作 $b =mysql_fetch_row( $result ); //转成数组 echo "<form action=\"xiugai1.php?code={ $a }\"method=\"post\"> <div>民族代号<input type=\"text\" name=\"code\" value=\"{ $b [0]}\" readonly=\"readonly\"/></div> <div>民族名称<input type=\"text\" name=\"name\" value=\"{ $b [1]}\"/></div> <input type=\"submit\" value=\"确认\"/> </form>"; ?> |
4-1修改处理页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php $code = $_GET [ "code" ]; $a = $_POST [ "code" ]; $b = $_POST [ "name" ]; $connect =@mysql_connect( "localhost" , "root" , "" ); mysql_select_db( "1" , $connect ); $sql = "update nation set name='{$b}' where code='{$code}'" ; $result =mysql_query( $sql ); if ( $result ) { header( "location:xianshi.php" ); } else { echo "修改失败" ; } ?> |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步