PHP在Mysql数据库中查询的方法

每种方式都有两种方法:1.过程方法 2.面向对象的方法

在使用面向对象连接数据库时要用


而使用过程方法连接数据库时


在查询时有以下几种方法,在此不再特别标明是过程方法还是面向对象

num_rows;//$num_results=mysqli_num_rows($result)
//查询返回的每一行数据,这里一般用循环结构
//每行返回一个数组,每个关键词为一个属性名 通过 $row['字段名']调用
$row=$result->fetch_assoc();//$row=mysqli_fetch_assoc($result);
//每行返回一个列举数组  可以通过row[0],row[1]等等调用
$row=$result->fetch_row();//$row=mysqli_fetch_row($result); $row=mysqli_fetch_arry($result);
//返回一行到一个对象中 用法$row->字段名
$row=$result->fetch_object();//$row=mysqli_fetch_object($result);

?>

wordpress查询获取文章、页面

[php]
<?
//获取特定分类文章名称
query_posts(‘cat=15&showposts=10’);//如果不设定文章显示数量,默认显示5篇
//获取相关页面
query_posts(‘p=3’);//page_id=3
query_posts(‘page_id=3’);
query_posts(‘pagename=about’);
//从主页中移除特定分类,本例中移除ID为3的分类
if(is_home()){
query_posts(‘cat=-3’);
}
//获取文章,采用循环
$posts=get_posts(‘numberposts=15,category=,off=,orderby=,category_name=’);
foreach($posts as $post):
setup_postdata($post);
the_permalink();//获取文章链接
the_title();//获取文章标题
the_content();//获取文章内容
endforeach;

?>

[/php]

解决数据库或网页显示乱码问题

网页显示问题有两种可能:

1.网页编码问题

(1)设置:<meta http-equiv=”content-Type” content=”text/html; charset=gb2312″> 或:<meta http-equiv=”content-Type” content=”text/html; charset=utf-8″>

(2)用编辑器重新打开网页源程序,另存为utf-8编码。

(3)php源程序 头部加上Header(“Content-type:text/html;charset=utf-8”);

2.数据库乱码问题

加一条命令“set names utf8”