欢迎光临
我们一直在努力

PHP连接MySQL数据库的代码描述

以下的文章主要讲述的是PHP连接MySQL数据库的实际操作步骤以及实际应用代码的描述,其实PHP连接MySQL是一件非常简单的事情,但是我们必须注意的是编码问题 ,编码不正确将会导致乱码问题。

连接数据库代码如下: 

代码 


 
  1. <?php 
  2. $link=MySQL_connect("localhost","root","123")  
  3. or die("connect to database failed");  
  4. $result=MySQL_query("create database trampt")  
  5. or die("query failed");  
  6. MySQL_select_db("trampt");  
  7. MySQL_query("set names UTF8");  
  8. $result=MySQL_query("create table Users(userid int auto_increment primary key,username varchar(50),password varchar(50))")  
  9. or die("query failed");  
  10. MySQL_query("insert into users(username,password) values('博客园','123')")  
  11. or die("insert record failed");  
  12. MySQL_close($link);  
  13. ?> 

读取数据库代码如下:


 
  1. <?php 
  2. require("inc/conn.php");  
  3. $result=MySQL_query("select * from users");  
  4. while($ob=MySQL_fetch_object($result))  
  5. {  
  6. echo "user name is : ".$ob->username."<br/>";  
  7. }  
  8. MySQL_close($link);  
  9. ?> 

注意编码问题的设置: 1 数据库设置utf-8的编码 2 页面编码设置为utf-8 3使用MySQL_query(“set names UTF8”)这样就避免了乱码问题。

以上的相关内容就是对PHP连接MySQL数据库的介绍,望你能有所收获。

【编辑推荐】

  1. MySQL DriverCS来连接MySQL的代码示例
  2. MySQL 游标的具体使用方案
  3. MySQL show的实际操作用法
  4. 对MySQL DELETE语法的详细解析
  5. MySQL数据库中的5种数据类型简介

赞(0) 打赏
未经允许不得转载:九八云安全 » PHP连接MySQL数据库的代码描述

评论 抢沙发