Maison > Article > développement back-end > 第十六章 PHP 操作MySQL_PHP教程
学习要点:
1.PHP 连接到MySQL
2.增删改查
3.其他常用函数
如果你已经具有了使用PHP、SQL 和MySQL 的丰富经验,现在就可以把所有这些技术
组合在一起。PHP 与MySQL 之间稳固的集成只是众多程序员采纳它的一个原因,还有一个
原因就是,它如此的简单方便。
一.PHP连接到MySQL
这里,我们全面采用UTF-8 编码。
设置Zend Stduio 的编码:Window -> Preferences -> Workspace
标头设置,让火狐和IE 保持编码统一:
<?<span php </span><span header</span>('Content-Type:text/html; charset=utf-8'<span ); </span>?>
连接MySQL
<?<span php </span><span $conn</span> = @<span mysql_connect</span>(DB_HOST,DB_USER,<span DB_PASSWORD) or </span><span die</span>('数据库连接失败!错误信息:'.<span mysql_error</span><span ()); </span>?>
数据库连接参数,可以用常量存储,这样就不能修改,更加安全。
<?<span php </span><span define</span>('DB_USER','root'<span ); </span><span define</span>('DB_PASSWORD','yangfan'<span ); </span><span define</span>('DB_HOST','localhost'<span ); </span><span define</span>('DB_NAME' ,'school'<span ); </span>?>
选择你所需要的数据库
<?<span php @</span><span mysql_select_db</span>(DB_NAME) or <span die</span>('数据库找不到!错误信息:'.<span mysql_error</span><span ()); </span>?>
设置字符集,如果是GBK,直接设置SET NAMES GBK 即可
<?<span php @</span><span mysql_query</span>('SET NAMES UTF8') or <span die</span>('字符集设置错误'<span ); </span>?>
获取记录集
<?<span php </span><span $query</span> = "SELECT * FROM grade"<span ; </span><span $result</span> = @<span mysql_query</span>(<span $query</span>) or <span die</span>('<span SQL 语句有误!错误信息: </span>'.<span mysql_error</span><span ()); </span>?>
输出一条记录
<?<span php </span><span print_r</span>(<span mysql_fetch_array</span>(<span $result</span>,<span MYSQL_ASSOC)); </span>?>
释放结果集资源
<?<span php </span><span mysql_free_result</span>(<span $result</span><span ); </span>?>
关闭数据库
<?<span php </span><span mysql_close</span>(<span $conn</span><span ); </span>?>
二.增删改查
新增数据
<?<span php </span><span $query</span> = "<span INSERT INTO grade (name,email,point,regdate) VALUE ('李炎恢','yc60.com@gmail.com',,NOW())</span>"<span ; @</span><span mysql_query</span>(<span $query</span>) or <span die</span>('添加数据出错:'.<span mysql_error</span><span ()); </span>?>
修改数据
<?<span php </span><span $query</span> = "UPDATE grade SET name='小可爱' WHERE id=6"<span ; @</span><span mysql_query</span>(<span $query</span>) or <span die</span>('修改出错:'.<span mysql_error</span><span ()); </span>?>
删除数据
<?<span php </span><span $query</span> = "DELETE FROM grade WHERE id=6"<span ; @</span><span mysql_query</span>(<span $query</span>) or <span die</span>('删除错误:'.<span mysql_error</span><span ()); </span>?>
显示数据
<?<span php </span><span $query</span> = "SELECT id,name,email,point FROM grade"<span ; </span><span $result</span> = @<span mysql_query</span>(<span $query</span>) or <span die</span>('查询语句出错:'.<span mysql_error</span><span ()); </span><span while</span> (!!<span $row</span> = <span mysql_fetch_array</span>(<span $result</span><span )) { </span><span echo</span> <span $row</span>['id'].'----'.<span $row</span>['name'].'----'.<span $row</span>['email'].'----'.<span $row</span>['point'<span ]; </span><span echo</span> '<br />'<span ; } </span>?>
三.其他常用函数
mysql_fetch_row():从结果集中取得一行作为枚举数组
mysql_fetch_assoc(): 从结果集中取得一行作为关联数组
mysql_fetch_array(): 从结果集中取得一行作为关联数组,或数字数组,或二者兼有
mysql_fetch_lengths (): 取得结果集中每个输出的长度
mysql_field_name(): 取得结果中指定字段的字段名
mysql_num_rows(): 取得结果集中行的数目
mysql_num_fields():取得结果集中字段的数目
mysql_get_client_info(): 取得MySQL 客户端信息
mysql_get_host_info(): 取得MySQL 主机信息
mysql_get_proto_info(): 取得MySQL 协议信息
mysql_get_server_info(): 取得MySQL 服务器信息
注:文章出自李炎恢PHP视频教程,本文仅限交流使用,不得用于商业用途,否则后果自负。