



免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
PHP连接MySQL使用PHP访问MySQL访问前要先过滤用户输入的字符串.用到的函数:trim()get_magic_quotes_gpc()addslashes()stripslashes()htmlspecialchars() - 将HTML特定的符号进行转换$new = htmlspecialchars(Test, ENT_QUOTES);echo $new; / 输出为:<a href=test>Test</a>连接MySQL构造mysqli类,这时可以通过调用对象的方法(类似于$db-close() )来查询$mysqli= new mysqli(localhost, my_user, my_password, my_db, 8000);/$ mysqli是一个对象/mysqli是一个类,构造函数的参数依次为 : 主机,用户,密码,数据库,端口,Socket号一般来说要加止抑制错误符号 $ mysqli = new mysqli(localhost, my_user, my_password, my_db, 8000);/连接是否成功可以通过函数来判断亦可以使用过程方法,这时要查询就要使用类似于mysqli_close($link) 的方式了$link= mysqli_connect (localhost, my_user, my_password, my_db, 8000);/$link是一个资源检测是否连接成功:if (!$mysqli) /连接时使用 抑制错误,此时再判断退出(或继续) die(Connect Error: . mysqli_connect_errno();/die相当于exit()函数或者if (mysqli_connect_errno()/不论是对象连接还是过程连接,都可以用此函数返回一个连接错误值echo ”Error! ”;选择数据库$mysqli-select_db(world);/相当于SQL语句use dbname;或mysqli_select_db($link, world);查询数据库$result = $mysqli-query(CREATE TEMPORARY TABLE myCity LIKE City);/或者是:$mysqli-query($query) 参数可以是变量或$result = mysqli_query($link, SELECT * FROM City, MYSQLI_USE_RESULT);发送给MySQL的查询不需要在末尾加分号检索查询结果返回结果的行数(记录数)$row_cnt = $result-num_rows;或$row_cnt = mysqli_num_rows($result);返回行(记录)n fetch_assoc -以相关数组返回;以key=value的形式while ($row = $result-fetch_assoc() /返回一条记录,当没有记录时返回NULL printf (%s (%s)n, $rowName, $rowCountryCode);/打印结果 或$row = mysqli_fetch_assoc($result);一般的在遍历记录前,要先进行整理echo stripslashes($rowName);/把多余的 删除while ($row = $result-fetch_row() printf (%s (%s)n, $row0, $row1); n fetch_row - 以枚举形式返回,类似于 $row0,$rou1的形式while ($row = mysqli_fetch_row($result) printf (%s (%s)n, $row0, $row1);n fetch_object - 将一行取回到一个对象中;访问时通过 $obj-name , $obj-Address 来实现while ($obj = $result-fetch_object() printf (%s (%s)n, $obj-Name, $obj-Address);n MySQLi_result类的属性、函数说明:mysqli_result-current_field Get current field offset of a result pointermysqli_result:data_seek Adjusts the result pointer to an arbitary row in the resultmysqli_result:fetch_all Fetches all result rows as an associative array, a numeric array, or bothmysqli_result:fetch_array Fetch a result row as an associative, a numeric array, or bothmysqli_result:fetch_assoc Fetch a result row as an associative array(以相关数组返回;即以key=value的形式)mysqli_result:fetch_field_direct Fetch meta-data for a single fieldmysqli_result:fetch_field Returns the next field in the result setmysqli_result:fetch_fields Returns an array of objects representing the fields in a result setmysqli_result:fetch_object Returns the current row of a result set as an object(返回对象;访问时通过 $obj-name , $obj-Address 来实现)mysqli_result:fetch_row Get a result row as an enumerated arraymysqli_result-field_count Get the number of fields in a resultmysqli_result:field_seek Set result pointer to a specified field offsetmysqli_result:free Frees the memory associated with a resultmysqli_result-lengths Returns the lengths of the columns of the current row in the result setmysqli_result-num_rows Gets the number of rows in a result*相关数组即索引数组,即数组的元素以: key=value 的形式存储从数据库断开:先释放结果,再断开数据库$result-free();$mysqli-close();或mysqli_free_result ($result );/注意,释放结果的过程函数只有这一个mysqli_close ( $link );严格来说,当一个脚本执行完毕,数据库会被自动释放。将新信息存入数据库n affected_rows (mysqli_affected_rows) - 返回操作的行数;通常在INSERT、UPDATE、DELETE中使用此函数,而在SELECT语句中使用num_rows函数来得到行数if (!get_magic_quotes_gpc()/存入数据前确保数据库的安全性$isbn = addslashes($isbn);$author = addslashes($author);$title = addslashes($title);$price = doubleval($price); $query = in
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 汽车机械制图试卷及答案
- 吊车指挥教学题库及答案
- 摩托车轮毂新建项目技术方案
- 绿化工程施工组织设计小包
- 环保设备安装与调试实施方案
- 离婚协议男方净身出户房产过户操作流程模板
- 离婚协议违约金条款的解除与变更规定
- 生源地信用助学贷款合同示范文本(专科)
- 离婚双方子女抚养权变更及经济补偿补充协议
- 离婚无子女子女监护协议范本与财产分割专项服务
- 八年级劳动课下册教案
- 人工动静脉瘘狭窄查房
- 15D501 建筑物防雷设施安装
- 加药装置技术规范书2014.1.16
- 水利工程中挡土墙的选型与稳定性验算
- 四年级语文下册看拼音写词语-田字格
- 重庆出版社综合实践四年级上全册教案
- 《先进制造技术》课件(全套)
- 花瓣凸台四方型腔铣削加工-数控机床毕业论文
- 一级建造师考试《水利水电工程管理与实务》电子书
- 记承天寺夜游(王崧舟)
评论
0/150
提交评论