php鏈接mysql必備條件:

創(chuàng)新互聯(lián)公司主營羅定網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都App制作,羅定h5成都微信小程序搭建,羅定網(wǎng)站營銷推廣歡迎羅定等地區(qū)企業(yè)咨詢
已安裝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數(shù)據(jù)庫:
因為連接數(shù)據(jù)庫需要較長的時間和較大的資源開銷,所以如果在多個網(wǎng)頁中都要頻繁地訪問數(shù)據(jù)庫,則可以建立與數(shù)據(jù)庫的持續(xù)連接。即調(diào)用mysql_pconnect()代替mysql_connect()。
基本步驟:
1.連接服務(wù)器:mysql_connect();
2.選擇數(shù)據(jù)庫:mysql_select_db();
3.執(zhí)行SQL語句:mysql_query();
查詢:select
顯示:show
插入:insert into
更新:update
刪除:delete
4.關(guān)閉結(jié)果集:mysql_free_result($result);
5.關(guān)閉數(shù)據(jù)庫:mysql_close($link);
?php
$dbhost = 'localhost'; ?// mysql服務(wù)器主機地址
$dbuser = 'root'; ? ? ? ? ? ?// mysql用戶名
$dbpass = '123456'; ? ? ? ? ?// mysql用戶名密碼
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn ){
die('Could not connect: ' . mysqli_error());
}
echo '數(shù)據(jù)庫連接成功!';
mysqli_close($conn);
?
下面是說明:
PHP 提供了 mysqli_connect() 函數(shù)來連接數(shù)據(jù)庫。該函數(shù)有 6 個參數(shù),在成功鏈接到 MySQL 后返回連接標(biāo)識,失敗返回 FALSE 。
語法
mysqli_connect(host, username, password, dbname,port, socket);
參數(shù)說明:
參數(shù)? ? ? ? ? ? ? 描述
host? ? ? ? ? ? ?可選。規(guī)定主機名或 IP 地址。
username? ? 可選。規(guī)定 MySQL 用戶名。
password? ? ?可選。規(guī)定 MySQL 密碼。
dbname? ? ? ?可選。規(guī)定默認(rèn)使用的數(shù)據(jù)庫。
port? ? ? ? ? ? ?可選。規(guī)定嘗試連接到 MySQL 服務(wù)器的端口號。
socket 可選。規(guī)定 socket 或要使用的已命名 pipe。
新聞標(biāo)題:php怎么進mysql,php怎么引入php
瀏覽路徑:http://chinadenli.net/article1/dsiecod.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、營銷型網(wǎng)站建設(shè)、外貿(mào)建站、網(wǎng)站改版、自適應(yīng)網(wǎng)站、網(wǎng)站維護
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)