当前位置:首页 » 编程语言 » php数据库连接

php数据库连接

发布时间: 2022-01-08 18:18:24

php怎么连接数据库

1、数据库连接第一步:配置mysql_connect()的参数
参数依次为:主机地址,用户名,用户密码
2、mysql_pconnect()与mysql_connect()是不一样的,pconnect顾名思义是持久连接
3、服务器连接成功后,需要你选择你需要用的数据库
4、使用mydql_close()可以关闭数据库连接资源,避免长时间占用启用资源消耗
5、mysqli_connect( )是mysql连接的另一种方式,参数形式一样
6、首次使用mysql连接数据库时,要记得使用输入逻辑判断,服务器连接不成功或者选择数据库不成功,都要用Mysql_error或者mysql_errno来报错
7、mysql的报错,能够帮助你准确地定位到错误发生在哪里。

Ⅱ php怎么连接mysql数据库啊

mysql_connect('服务器名','用户名','密码') or die('链接数据库出错'); //链接数据库
mysql_select_db('数据库名') or die('选择数据库失败');; //选择你想要两件的数据库

Ⅲ PHP连接数据库

<?php

$Link=mysql_connect('localhost','root','123456')ordie('数据库服务器连接失败,错误信息:'.mysql_error());

mysql_select_db('test')ordie('数据库选取失败,错误信息:'.mysql_error());


mysql_query('setnamesgbk');

?>
<?php

include_once'conn.php';//引入连接数据库

if(!empty($_POST['sub'])){

$title=$_POST['title'];

$con=$_POST['con'];

$sql="insertinto`willie`(`title`,`dates`,`contents`)values('$title',now(),'$con')";
echomysql_query($sql)?'插入成功':'插入失败,错误信息:'.mysql_error();}

Ⅳ 用php如何连接MySQL数据库

php链接mysql必备条件:

  • 已安装mysql数据库;

  • 检查php环境是否已开启mysql扩展(一般情况下是开启的);

    检查方法:a.使用phpinfo();函数,看有没有mysql项;b.打开php.ini文件,检查php_mysql.dll前分号是否已取掉。

  • php链接代码如下:

    <?php
    //设置编码格式header("Content-type:text/html;charset=utf-8");//定义数据库主机地址$host="localhost";//定义mysql数据库登录用户名$user="root";//定义mysql数据库登录密码$pwd="";//链接数据库$conn=mysql_connect($host,$user,$pwd);//对连接进行判断if(!$conn){die("数据库连接失败!".mysql_errno());}else{echo"数据库连接成功!";}?>

Ⅳ php连接mysql

public function getInfo(){
$this->userName=$this->userInfo["name"];
$this->userPSW=$this->userInfo["password"];
$this->userAge=$this->userInfo["age"];
$this->userGrade=$this->userInfo["grade"];
}
你这里不对吧,应该是
$this->userName=$this->userInfo[0]["name"];
$this->userPSW=$this->userInfo[0]["password"];
$this->userAge=$this->userInfo[0]["age"];
$this->userGrade=$this->userInfo[0]["grade"];
就算你只取一条数据,也还是一个数组,所以要加下标0

Ⅵ 如何通过php实现mysql数据库连接

php链接mysql必备条件:

  • 已安装mysql数据库;

  • 检查php环境是否已开启mysql扩展(一般情况下是开启的);

    检查方法:a.使用phpinfo();函数,看有没有mysql项;b.打开php.ini文件,检查php_mysql.dll前分号是否已取掉。

  • php链接代码如下:

    4
    <?php//设置编码格式header("Content-type:text/html;charset=utf-8");//定义数据库主机地址$host="localhost";//定义mysql数据库登录用户名$user="root";//定义mysql数据库登录密码$pwd="";//链接数据库$conn=mysql_connect($host,$user,$pwd);//对连接进行判断if(!$conn){die("数据库连接失败!".mysql_errno());}else{echo"数据库连接成功!";}?>

Ⅶ 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>
<!-- 为防止出现意外,请按照此标准顺序书写.其实也无所谓了 -->
<host>localhost</host>
<user>root</user>
<password>123456</password>
<db>test</db>
<port>3306</port></mysql>12345678910

解析

<?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>读取数据库配置文件信息出错!</mark><br />" );
} return $dbconfig;
}
}

数据库连接池

对于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 ( "<mark>utils.php文件丢失,无法进行配置文件的初始化操作!</mark><br />" );
}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>连接数据库失败!</mark><br />" );
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>数据库连接池已满</mark><br />" );
} else {
array_push ( $this->dbpool, $conn );
}
}
}

Ⅷ PHp如何连接数据库

不知道你用的是什么数据库,如果是Mysql的话,一个php函数就搞定了
mysql_connect('host',
'username',
'password');
三个参数分别是
数据库主机
ip,
数据库用户名,
数据库密码

Ⅸ php如何连接数据库

$mysql_host="localhost";地址
$mysql_user="root";用户名
$mysql_password="123";密码
$mysql_database="001online";数据库名
$conn=mysql_connect("$mysql_host","$mysql_user","$mysql_password");
if(!$conn){die("连接数据库失败:".mysql_error());}
mysql_select_db("$mysql_database",$conn);
mysql_query("setcharacterset'utf-8'");
mysql_query("setnames'utf-8'");

Ⅹ 怎么将php与数据库连接

php链接mysql必备条件:
已安装mysql数据库;

检查php环境是否已开启mysql扩展(一般情况下是开启的);
检查方法:a.使用phpinfo();函数,看有没有mysql项;b.打开php.ini文件,检查php_mysql.dll前分号是否已取掉。
php链接代码如下:
<?php
//设置编码格式
header("Content-type:text/html;charset=utf-8");

//定义数据库主机地址
$host="localhost";

//定义mysql数据库登录用户名
$user="root";

//定义mysql数据库登录密码
$pwd="";

//链接数据库
$conn = mysql_connect($host,$user,$pwd);

//对连接进行判断
if(!$conn){
die("数据库连接失败!".mysql_errno());
}else{

echo "数据库连接成功!";
}
?>

热点内容
单片机android 发布:2024-09-20 09:07:24 浏览:765
如何提高三星a7安卓版本 发布:2024-09-20 08:42:35 浏览:664
如何更换服务器网站 发布:2024-09-20 08:42:34 浏览:311
子弹算法 发布:2024-09-20 08:41:55 浏览:289
手机版网易我的世界服务器推荐 发布:2024-09-20 08:41:52 浏览:817
安卓x7怎么边打游戏边看视频 发布:2024-09-20 08:41:52 浏览:162
sql数据库安全 发布:2024-09-20 08:31:32 浏览:94
苹果连接id服务器出错是怎么回事 发布:2024-09-20 08:01:07 浏览:507
编程键是什么 发布:2024-09-20 07:52:47 浏览:658
学考密码重置要求的证件是什么 发布:2024-09-20 07:19:46 浏览:481