原生PHP连接数据库并查询数据
<?php
header("Content-Type: text/html; charset=UTF-8");
$dbhost = 'localhost';
$dbuser = 'root'; // 用户名
$dbpass = 'root'; //密码
$dbname = 'db_name'; // 数据库名
$connect = mysql_connect($dbhost, $dbuser, $dbpass, $dbname) or die("数据库连接失败");
mysql_query("set names utf8");
mysql_select_db($dbname); // 打开数据库
// 站点配置
function site($varname)
{
$sitesql = "select `value` from `sysconfig` where `varname`='" . $varname . "'";
$sitequery = mysql_query($sitesql);
$site = mysql_fetch_assoc($sitequery);
return $site['value'];
}
// 图片路径截取
function picurl($picurl)
{
if (strpos($picurl, 'templets')) {
$picurl = str_replace("/templets", "http://domain.com/templets", $picurl);
} else if (strpos($picurl, 'uploads')) {
$picurl = str_replace("/uploads", "http://domain.com/uploads", $picurl);
} else {
$picurl = substr($picurl, 0, -3);
}
return $picurl;
}
// 文章列表
$pid = !empty($_GET['pid']) ? $_GET['pid'] : 1;
@include("./page_class.php");
$strsql = "select `id`,`pubdate`,`title` from `archives` where `typeid`=$pid order by `id` desc";
$result = mysql_query($strsql);
$total = mysql_num_rows($result);
pageft($total, 8);
if ($firstcount >= 0) {
$strsql .= " limit " . $firstcount . ",8";
$query = mysql_query($strsql);
while ($row = mysql_fetch_assoc($query)) {
echo '<li class="am-g am-list-item-dated">
<a href="show.php?iid=' . $row['id'] . '" class="am-list-item-hd">' . $row['title'] . '</a>
<span class="am-list-date">' . date("Y-m-d", $row['pubdate']) . '</span>
</li>';
}
}
// 单页内容
function page($pid, $pagecat)
{
$pagesql = "select " . $pagecat . " from `arctype` where id='" . $pid . "'";
$pagequery = mysql_query($pagesql);
$page = mysql_fetch_assoc($pagequery);
if (strpos($page[$pagecat], 'uploads')) {
$page[$pagecat] = str_replace("/uploads", "http://domain.com/uploads", $page[$pagecat]);
}
return $page[$pagecat];
}
// 文章内容
function article($id, $catname)
{
if ($catname == 'typeid') {
$catname = 'archives.typeid';
}
$artsql = "select " . $catname . " from `archives` left join `addonarticle` on archives.id=addonarticle.aid where id='" . $id . "'";
$artquery = mysql_query($artsql);
$article = mysql_fetch_assoc($artquery);
if ($catname == 'archives.typeid') {
$catname = 'typeid';
}
if (strpos($article[$catname], 'uploads')) {
$article[$catname] = str_replace("/uploads", "http://lyhaiye.com/uploads", $article[$catname]);
}
return $article[$catname];
}
// 产品内容
function product($id, $catname)
{
$artsql = "select * from `lrf_model_picture` where id='" . $id . "'";
$proquery = mysql_query($artsql);
$product = mysql_fetch_assoc($proquery);
return $product[$catname];
}
// 文章上一页,下一页
function laneart($id, $typeid, $catname, $order)
{
if (trim($order) == 'asc') {
$tt = '>';
} else {
$tt = '<';
}
$article = "select title,id from `archives` where typeid='" . $typeid . "' and id " . $tt . $id . " order by archives.id " . $order . " limit 1";
$artquery = mysql_query($article);
$rows = mysql_fetch_assoc($artquery);
if ($catname == 'id') {
if (empty($rows[$catname])) {
$rows[$catname] = $id;
}
}
if ($catname == 'title') {
if (empty($rows[$catname])) {
$rows[$catname] = '无';
}
}
return $rows[$catname];
}
// 产品上一页,下一页
function lanepic($id, $cid, $catname, $order)
{
if (trim($order) == 'asc') {
$tt = '>';
} else {
$tt = '<';
}
$article = "select title,id from `lrf_model_picture` where cid='" . $cid . "' and id " . $tt . $id . " order by id " . $order . " limit 1";
$artquery = mysql_query($article);
$rows = mysql_fetch_assoc($artquery);
if (empty($rows[$catname])) {
if ($catname == 'id') {
$rows[$catname] = $id;
}
if ($catname == 'title') {
$rows[$catname] = '无';
}
}
return $rows[$catname];
}