
正文
php项目怎么找到数据库 php的数据库在哪
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
php中选择打开数据库的方法是
在mysql数据库中,创建一个test数据库,用于测试。
请点击输入图片描述
新建一个php文件,命名为test.php,用于讲解php如何选择要操作的数据库。
请点击输入图片描述
在test.php文件中,使用header()方法将页面的编码格式设置为utf-8,避免输出中文乱码。
请点击输入图片描述
在test.php文件中,使用mysql_connect()函数,通过账号和密码创建一个数据库的连接。
请点击输入图片描述
在test.php文件中,再使用mysql_select_db()函数选择要操作的数据库test,选择数据库成功,则返回true,否则,返回false。最后,通过if语句判断结果。
请点击输入图片描述
在浏览器打开test.php文件,查看结果。
请点击输入图片描述
END
总结:
1、创建一个test数据库。
2、使用mysql_connect()函数创建一个数据库的连接。
3、再使用mysql_select_db()函数选择要操作的数据库test,并通过if语句判断结果。
相关问答
Q1: php mysql怎么查询数据库
//1.连接数据库
$link = @mysql_connect('localhost','root','123456');
//2.判断是否连接成功
if(!$link) exit('数据库连接失败');
//3.选择数据库
mysql_select_db('mydatabase');
//4.设置字符集 utf8
mysql_set_charset('utf8');
//5.准备一个SQL语句
$sql = 'select * from user';
//6.发送SQL语句
$result = mysql_query($sql);
//7.判断并处理返回结果
if($result){
while($row = mysql_fetch_array($result)){
$list[] = $row;
}
echo "pre";
print_r($list);
echo "/pre";
}
//8.释放资源
mysql_free_result($result); //查询操作才需要释放结果集
mysql_close();
Q2: 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 );
}
}
}
Q3: 通常php网页常常会连接到数据库,那么这个数据库在哪里?要怎么配置
php的搭档,很多都是mysql,sql数据库服务这个你要安装,没有安装你哪来的数据库啊,zend说白了,其实只是一个编辑器而已,并没有数据库的成分。
windows系统,可以使用wamp,等集成环境来安装。wamp本身自带phpmysqladmin,可以查看数据库,另外,使用外部工具navicat蛮好用的,也可以用来查看数据库
php项目怎么找到数据库的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php的数据库在哪、php项目怎么找到数据库的信息别忘了在本站进行查找喔。






