
正文
php的数据库网页 php的数据库网页怎么打开
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
(PHP)从数据库查询出产品数据 怎样把它很有整齐的显示在网页上 ?
?php
//第一步:透过代理获取用户真实的IP地址
$theip=getip();
function getIP()
{
static $realip;
if (isset($_SERVER)){
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
$realip = $_SERVER["HTTP_CLIENT_IP"];
} else {
$realip = $_SERVER["REMOTE_ADDR"];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")){
$realip = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}
//第二步:通过读取IP地址库,找到所属地区
//假设表名为ip,字段有begin(起始),end(结束),area(地区代号)
$dblink=@mysql_connect("数据库服务器名","数据库用户名","数据库密码");
$dbselect=@mysql_select_db(数据库名);
if (!$dbselect){
die("数据库无法读取");
}
$result=mysql_query("select * from `ip` where `begin`=`$theip` and `end`=`$theip`");
$num=mysql_num_rows($result);
if($num0){
$info=mysql_fetch_array($result);
$page=$info['area'];
else{
$page='default'; //如果IP不在数据库中,则用缺省的页面
}
//第三步:转到相关页面,如代号为"js",转到"js.php"页面
$page.='.html'; //后缀你可以自己取
echo "script language=\"javascript\"window.location.replace(\"$page\")/script";
?
相关问答
Q1: 怎样用php+mysql 做一个查询的网页
首先搭建一个PHP环境php的数据库网页,我用的wamp
然后比如php的数据库网页你的数据库位置是本地localhost
数据库用户名是root
数据库密码是123456
数据库名是mydb
数据库里有个表mytab
有3个字段
id(主键) name sno
1 张三 123
2 李四 456
然后在项目根目录php的数据库网页,新建一个文件: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/html; charset=UTF-8"
title实现最简单的php网页+mysql查询功能/title
/head
body
?php
echo "pre";
print_r($obj);
?
/body
/html
之后就能够看到结果php的数据库网页了
Q2: PHP网页怎么连接到MYSQL数据库
你写的这个只是数据库连接的代码php的数据库网页,你只是连接了数据库php的数据库网页,可以对你的“”数据库进行"CURD"操作php的数据库网页,$conn返回的是resourcephp的数据库网页,mysql_select_db()和
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('错误提示:链接数据库失败php的数据库网页!');
}
}
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('错误提示:设置编码失败!');
}
}
}
}
Q3: PHP网站怎么连接到数据库?
常规方式
常规方式就是按部就班的读取文件了。其余的话和上述方案一致。
// 读取配置文件内容
$handle = fopen("filepath", "r"); $content = fread($handle, filesize("filepath"));123
PHP解析XML
上述两种读取文件,其实都是为了PHP解析XML来做准备的。关于PHP解析XML的方式的博客有很多。方式也有很多,像simplexml,XMLReader,DOM啦等等。但是对于比较小型的xml配置文件,simplexml就足够了。
配置文件
?xml version="1.0" encoding="UTF-8" ?mysql
!-- 为防止出现意外,请按照此标准顺序书写.其实也无所谓了 --
hostlocalhost/host
userroot/user
password123456/password
dbtest/db
port3306/port/mysql12345678910
解析
?php/**
* 作为解析XML配置文件必备工具
*/class XMLUtil {
public static $dbconfigpath = "./db.config.xml"; public static function getDBConfiguration() {
$dbconfig = array (); try { // 读取配置文件内容
$handle = fopen(self::$dbconfigpath, "r"); $content = fread($handle, filesize(self::$dbconfigpath)); // 获取xml文档根节点,进而获取相关的数据库信息
$mysql = simplexml_load_string($content); // 将获取到的xml节点信息赋值给关联数组,方便接下来的方法调用
$dbconfig['host'] = $mysql-host; $dbconfig['user'] = $mysql-user; $dbconfig['password'] = $mysql-password; $dbconfig['db'] = $mysql-db; $dbconfig['port'] = $mysql-port; // 将配置信息以关联数组的形式返回
return $dbconfig;
} catch ( Exception $e ) { throw new RuntimeException ( "mark读取数据库配置文件信息出错!/markbr /" );
} return $dbconfig;
}
}1234567891011121314151617181920212223242526272829
数据库连接池
对于PHP程序而言,优化永无止境。而数据库连接池就在一定程度上起到了优化的作用。其使得对用户的每一个请求而言,无需每次都像数据库申请链接资源。而是通过已存在的数据库连接池中的链接来返回,从时间上,效率上,都是一个大大的提升。
于是,这里简单的模拟了一下数据库连接池的实现。核心在于维护一个“池”。
从池子中取,用毕,归还给池子。
?php/**x
* PHP中的数据库 工具类设计
* 郭璞
* 2016年12月23日
*
**/class DbHelper { private $dbconfig; private $dbpool; public $poolsize; public function __construct($poolsize = 20) { if (! file_exists ( "./utils.php" )) { throw new RuntimeException ( "markutils.php文件丢失,无法进行配置文件的初始化操作!/markbr /" );
}else {
require './utils.php';
} // 初始化 配置文件信息
$this-dbconfig = XMLUtil::getDBConfiguration (); // 准备好数据库连接池“伪队列”
$this-poolsize = $poolsize;
$this-dbpool = array (); for($index = 1; $index = $this-poolsize; $index ++) {
$conn = mysqli_connect ( $this-dbconfig ['host'], $this-dbconfig ['user'], $this-dbconfig ['password'], $this-dbconfig ['db'] ) or die ( "mark连接数据库失败!/markbr /" );
array_push ( $this-dbpool, $conn );
}
} /**
* 从数据库连接池中获取一个数据库链接资源
*
* @throws ErrorException
* @return mixed
*/
public function getConn() { if (count ( $this-dbpool ) = 0) { throw new ErrorException ( "mark数据库连接池中已无链接资源,请稍后重试!/mark" );
} else { return array_pop ( $this-dbpool );
}
} /**
* 将用完的数据库链接资源放回到数据库连接池
*
* @param unknown $conn
* @throws ErrorException
*/
public function release($conn) { if (count ( $this-dbpool ) = $this-poolsize) { throw new ErrorException ( "mark数据库连接池已满/markbr /" );
} else {
array_push ( $this-dbpool, $conn );
}
}
}
php的数据库网页的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php的数据库网页怎么打开、php的数据库网页的信息别忘了在本站进行查找喔。






