mysql_query()返回的则是布尔类型,所以在浏览器预览的时候是没有任何内容的,有内容也只是一个TRUE
连接数据库的代码如下:
数据库 *** 作类
class
mysql
{
private
$db_host
//数据库主机
private
$db_user
//数据库用户名
private
$db_pwd
//数据库密码
private
$db_database
//数据库名
private
$conn
//数据库连接标识
private
$sql
//sql执行的语句
private
$result
//query的资源标识符
private
$coding
//数据库编码,gbk,utf8,gb2312
private
$show_error
=
true
//本地调试使用,打印错误
public
function
__construct($db_host,
$db_user,
$db_pwd,
$db_database,
$coding)
{
$this->db_host
=
$db_host
$this->db_user
=
$db_user
$this->db_pwd
=
$db_pwd
$this->db_database
=
$db_database
$this->coding
=
$coding
$this->connect()
}
private
function
connect()
{
$this->conn
=
@mysql_connect($this->db_host,
$this->db_user,
$this->db_pwd)
if
(!$this->conn)
{
//show_error开启时,打印错误
if
($this->show_error)
{
$this->show_error('错误提示:链接数据库失败!')
}
}
if
(!@mysql_select_db($this->db_database,
$this->conn))
{
//打开数据库失败
if
($this->show_error)
{
$this->show_error('错误提示:打开数据库失败!')
}
}
if
(!@mysql_query("set
names
$this->coding"))
{
//设置编码失败
if
($this->show_error)
{
$this->show_error('错误提示:设置编码失败!')
}
}
}
}
首先搭建一个PHP环境,我用的wamp然后比如你的数据库位置是本地localhost
数据库用户名是root
数据库密码是123456
数据库名是mydb
数据库里有个表mytab
有3个字段
id(主键)name sno
1 张三 123
2 李四 456
然后在项目根目录,新建一个文件:index.php
<?php
//连接数据库
$con=mysqli_connect("localhost","root","123456","mydb")
//SQL语句
$sql="select * from mytab"
//执行SQL语句,结果保存到$arr
$obj=mysqli_query($con,$sql)
$arr=mysqli_num_rows($result)
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=UTF-8">
<title>实现最简单的php网页+mysql查询功能</title>
</head>
<body>
<?php
echo "<pre>"
print_r($obj)
?>
</body>
</html>
之后就能够看到结果了
网页文件head设置编码<meta http-equiv=”Content-Type” content=”text/htmlcharset=utf-8″ />
2.PHP页面在保存的时候使用utf-8编码保存,可以用记事本或convertz802转换文件
3.在MYSQL中新建数据库的时候数据库 选择UTF-8编码既字符集 设定为 utf-8_unicode_ci(Unicode (多语言), 不区分大小写),
库里面 表table的 整理 设置为 utf-8_general_ci
表里面的每个字段的 整理 都设置为 utf-8_general_ci
4.在PHP连接数据库的时候,也就是mysql_connect()之后加入utf8
以上四点即可实现全站utf-8编码,而且在数据库中也不会有中文乱码
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)