新聞中心
PHp如何連接數(shù)據(jù)庫?
PHP鏈接數(shù)據(jù)庫有幾種方式
創(chuàng)新互聯(lián)公司主營夏縣網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,App定制開發(fā),夏縣h5微信小程序開發(fā)搭建,夏縣網(wǎng)站營銷推廣歡迎夏縣等地區(qū)企業(yè)咨詢
mysqli:
?php?
$servername?=?"localhost";?
$username?=?"username";?
$password?=?"password";?
//?創(chuàng)建連接?
$conn?=?new?mysqli($servername,?$username,?$password);?
//?檢測連接?
if?($conn-connect_error)?{
die("連接失敗:?"?.?$conn-connect_error);?
}?
echo?"連接成功";?
?
也可以使用PDO進行鏈接,前提是你必須在php.ini中開啟PDO:
?php
$servername?=?"localhost";
$username?=?"username";
$password?=?"password";
try?{
$conn?=?new?PDO("mysql:host=$servername;dbname=myDB",?$username,?$password);
echo?"連接成功";?
}
catch(PDOException?$e)
{
echo?$e-getMessage();
}
?
建議使用PDO,功能更加強大,兼容各種數(shù)據(jù)庫
php如何連接數(shù)據(jù)庫
$mysql_host="localhost";??地址
$mysql_user="root";???????用戶名
$mysql_password="123";????密碼
$mysql_database="001online";???數(shù)據(jù)庫名
$conn=mysql_connect("$mysql_host","$mysql_user","$mysql_password");
if(!$conn){?die("連接數(shù)據(jù)庫失?。??.?mysql_error());}
mysql_select_db("$mysql_database",$conn);
mysql_query("set?character?set?'utf-8'");
mysql_query("set?names?'utf-8'");
php里面怎么鏈接數(shù)據(jù)庫?
php鏈接mysql必備條件:
已安裝mysql數(shù)據(jù)庫;
檢查php環(huán)境是否已開啟mysql擴展(一般情況下是開啟的);
檢查方法:a.使用phpinfo();函數(shù),看有沒有mysql項;b.打開php.ini文件,檢查php_mysql.dll前分號是否已取掉。
php鏈接代碼如下:
?php
//設(shè)置編碼格式
header("Content-type:text/html;charset=utf-8");
//定義數(shù)據(jù)庫主機地址
$host="localhost";
//定義mysql數(shù)據(jù)庫登錄用戶名
$user="root";
//定義mysql數(shù)據(jù)庫登錄密碼
$pwd="";
//鏈接數(shù)據(jù)庫
$conn = mysql_connect($host,$user,$pwd);
//對連接進行判斷
if(!$conn){
die("數(shù)據(jù)庫連接失??!".mysql_errno());
}else{
echo "數(shù)據(jù)庫連接成功!";
}
?
PHP+MySQL如何統(tǒng)計數(shù)據(jù)庫容量?
要想知道每個數(shù)據(jù)庫的大小的話,步驟如下:
1、進入information_schema 數(shù)據(jù)庫(存放了其他的數(shù)據(jù)庫的信息)
use information_schema;
2、查詢所有數(shù)據(jù)的大?。?/p>
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables;
3、查看指定數(shù)據(jù)庫的大小:
比如查看數(shù)據(jù)庫home的大小
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='home';
4、查看指定數(shù)據(jù)庫的某個表的大小
比如查看數(shù)據(jù)庫home中 members 表的大小
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='home' and table_name='members';
php連接數(shù)據(jù)庫服務(wù)器,然后選擇使用的數(shù)據(jù)庫名稱為information_schema,然后執(zhí)行查詢就行了??茨銌柕倪@個問題應(yīng)該不會不知道用php訪問數(shù)據(jù)庫吧。
如果你權(quán)限不夠的話可能只能對特定的數(shù)據(jù)庫的信息進行查詢。
PHP連接數(shù)據(jù)庫查詢條數(shù)的問題
php使用mysql查詢數(shù)據(jù)庫已經(jīng)有多少條數(shù)據(jù)使用sql的count函數(shù)實現(xiàn)。
示例代碼如下:
?php
//數(shù)據(jù)庫連接
$conn=mysql_connect("localhost","root","root");
if(!$conn){
die("對不起,數(shù)據(jù)庫連接失??! ").mysql_errno();
}
//選擇數(shù)據(jù)庫
mysql_select_db("testdb");
//sql語句
$sql="SELECT COUNT(*) AS count FROM user";
//執(zhí)行sql
$query=mysql_query($sql,$conn);
//對結(jié)果進行判斷
if(mysql_num_rows( $query)){
$rs=mysql_fetch_array($query);
//統(tǒng)計結(jié)果
$count=$rs[0];
標(biāo)題名稱:PHP統(tǒng)計連接數(shù)據(jù)庫,php訪問統(tǒng)計
轉(zhuǎn)載源于:http://ef60e0e.cn/article/dsgsspe.html