課程
/后端開發(fā)
/PHP
/PHP入門篇
怎么連接wamp里面的MySQL數(shù)據(jù)庫呀
2016-10-25
源自:PHP入門篇 2-4
正在回答
PHP要對數(shù)據(jù)庫進(jìn)行操作,首先要做的是與數(shù)據(jù)庫建立連接,通常我們使用mysql_connect函數(shù)進(jìn)行數(shù)據(jù)庫連接,該函數(shù)需要指定數(shù)據(jù)庫的地址,用戶名及密碼。
$host = 'localhost';$user = 'code1';$pass = '';$link = mysql_connect($host, $user, $pass);
PHP連接數(shù)據(jù)庫的方式類似于直接在命令行下通過進(jìn)行連接,類似:mysql -hlocalhost -ucode1 -p,當(dāng)連接成功以后,我們需要選擇一個操作的數(shù)據(jù)庫,通過mysql_select_db函數(shù)來選擇數(shù)據(jù)庫。
mysql_select_db('code1');
通常我們會先設(shè)置一下當(dāng)前連接使用的字符編碼,一般的我們會使用utf8編碼。
mysql_query("set names 'utf8'");
通過上面的步驟,我們就與數(shù)據(jù)庫建立了連接,可以進(jìn)行數(shù)據(jù)操作了。
$link=new PDO(mysql:host=localhost,root,root);
<?php$servername = "localhost";
$username = "username";
$password = "password";
?// 創(chuàng)建連接$conn = new mysqli($servername, $username, $password);
?// 檢測連接if ($conn->connect_error)
?{ ? ?die("連接失敗: " . $conn->connect_error);}
?echo "連接成功";?>
不知道對不對
舉報
PHP入門教程輕松學(xué)習(xí),行業(yè)大牛幫您快速掌握PHP編程基礎(chǔ)知識。
1 回答連接數(shù)據(jù)庫
4 回答數(shù)據(jù)庫問題???
2 回答數(shù)據(jù)庫打不開
1 回答數(shù)據(jù)庫拒絕訪問
2 回答假如變量值來自數(shù)據(jù)庫,數(shù)據(jù)庫中的內(nèi)容有$ 、"、;到時候不是亂成一鍋粥了嗎?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2016-10-25
PHP要對數(shù)據(jù)庫進(jìn)行操作,首先要做的是與數(shù)據(jù)庫建立連接,通常我們使用mysql_connect函數(shù)進(jìn)行數(shù)據(jù)庫連接,該函數(shù)需要指定數(shù)據(jù)庫的地址,用戶名及密碼。
$host = 'localhost';
$user = 'code1';
$pass = '';
$link = mysql_connect($host, $user, $pass);
PHP連接數(shù)據(jù)庫的方式類似于直接在命令行下通過進(jìn)行連接,類似:mysql -hlocalhost -ucode1 -p,當(dāng)連接成功以后,我們需要選擇一個操作的數(shù)據(jù)庫,通過mysql_select_db函數(shù)來選擇數(shù)據(jù)庫。
mysql_select_db('code1');
通常我們會先設(shè)置一下當(dāng)前連接使用的字符編碼,一般的我們會使用utf8編碼。
mysql_query("set names 'utf8'");
通過上面的步驟,我們就與數(shù)據(jù)庫建立了連接,可以進(jìn)行數(shù)據(jù)操作了。
2016-10-26
$link=new PDO(mysql:host=localhost,root,root);
2016-10-25
<?php$servername = "localhost";
$username = "username";
$password = "password";
?// 創(chuàng)建連接$conn = new mysqli($servername, $username, $password);
?// 檢測連接if ($conn->connect_error)
?{ ? ?die("連接失敗: " . $conn->connect_error);}
?echo "連接成功";?>
不知道對不對