PHP鏈接數(shù)據(jù)庫(kù)有幾種方式

創(chuàng)新互聯(lián)公司主營(yíng)夏縣網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,App定制開(kāi)發(fā),夏縣h5微信小程序開(kāi)發(fā)搭建,夏縣網(wǎng)站營(yíng)銷(xiāo)推廣歡迎夏縣等地區(qū)企業(yè)咨詢(xún)
mysqli:
?php?
$servername?=?"localhost";?
$username?=?"username";?
$password?=?"password";?
//?創(chuàng)建連接?
$conn?=?new?mysqli($servername,?$username,?$password);?
//?檢測(cè)連接?
if?($conn-connect_error)?{
die("連接失敗:?"?.?$conn-connect_error);?
}?
echo?"連接成功";?
?
也可以使用PDO進(jìn)行鏈接,前提是你必須在php.ini中開(kāi)啟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,功能更加強(qiáng)大,兼容各種數(shù)據(jù)庫(kù)
$mysql_host="localhost";??地址
$mysql_user="root";???????用戶(hù)名
$mysql_password="123";????密碼
$mysql_database="001online";???數(shù)據(jù)庫(kù)名
$conn=mysql_connect("$mysql_host","$mysql_user","$mysql_password");
if(!$conn){?die("連接數(shù)據(jù)庫(kù)失敗:"?.?mysql_error());}
mysql_select_db("$mysql_database",$conn);
mysql_query("set?character?set?'utf-8'");
mysql_query("set?names?'utf-8'");
php鏈接mysql必備條件:
已安裝mysql數(shù)據(jù)庫(kù);
檢查php環(huán)境是否已開(kāi)啟mysql擴(kuò)展(一般情況下是開(kāi)啟的);
檢查方法:a.使用phpinfo();函數(shù),看有沒(méi)有mysql項(xiàng);b.打開(kāi)php.ini文件,檢查php_mysql.dll前分號(hào)是否已取掉。
php鏈接代碼如下:
?php
//設(shè)置編碼格式
header("Content-type:text/html;charset=utf-8");
//定義數(shù)據(jù)庫(kù)主機(jī)地址
$host="localhost";
//定義mysql數(shù)據(jù)庫(kù)登錄用戶(hù)名
$user="root";
//定義mysql數(shù)據(jù)庫(kù)登錄密碼
$pwd="";
//鏈接數(shù)據(jù)庫(kù)
$conn = mysql_connect($host,$user,$pwd);
//對(duì)連接進(jìn)行判斷
if(!$conn){
die("數(shù)據(jù)庫(kù)連接失敗!".mysql_errno());
}else{
echo "數(shù)據(jù)庫(kù)連接成功!";
}
?
要想知道每個(gè)數(shù)據(jù)庫(kù)的大小的話(huà),步驟如下:
1、進(jìn)入information_schema 數(shù)據(jù)庫(kù)(存放了其他的數(shù)據(jù)庫(kù)的信息)
use information_schema;
2、查詢(xún)所有數(shù)據(jù)的大小:
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables;
3、查看指定數(shù)據(jù)庫(kù)的大小:
比如查看數(shù)據(jù)庫(kù)home的大小
select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='home';
4、查看指定數(shù)據(jù)庫(kù)的某個(gè)表的大小
比如查看數(shù)據(jù)庫(kù)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ù)庫(kù)服務(wù)器,然后選擇使用的數(shù)據(jù)庫(kù)名稱(chēng)為information_schema,然后執(zhí)行查詢(xún)就行了。看你問(wèn)的這個(gè)問(wèn)題應(yīng)該不會(huì)不知道用php訪問(wèn)數(shù)據(jù)庫(kù)吧。
如果你權(quán)限不夠的話(huà)可能只能對(duì)特定的數(shù)據(jù)庫(kù)的信息進(jìn)行查詢(xún)。
php使用mysql查詢(xún)數(shù)據(jù)庫(kù)已經(jīng)有多少條數(shù)據(jù)使用sql的count函數(shù)實(shí)現(xiàn)。
示例代碼如下:
?php
//數(shù)據(jù)庫(kù)連接
$conn=mysql_connect("localhost","root","root");
if(!$conn){
die("對(duì)不起,數(shù)據(jù)庫(kù)連接失敗! ").mysql_errno();
}
//選擇數(shù)據(jù)庫(kù)
mysql_select_db("testdb");
//sql語(yǔ)句
$sql="SELECT COUNT(*) AS count FROM user";
//執(zhí)行sql
$query=mysql_query($sql,$conn);
//對(duì)結(jié)果進(jìn)行判斷
if(mysql_num_rows( $query)){
$rs=mysql_fetch_array($query);
//統(tǒng)計(jì)結(jié)果
$count=$rs[0];
新聞名稱(chēng):PHP統(tǒng)計(jì)連接數(shù)據(jù)庫(kù),php訪問(wèn)統(tǒng)計(jì)
文章出自:http://chinadenli.net/article34/dsgsspe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、手機(jī)網(wǎng)站建設(shè)、App設(shè)計(jì)、動(dòng)態(tài)網(wǎng)站、面包屑導(dǎo)航、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
全網(wǎng)營(yíng)銷(xiāo)推廣知識(shí)