當前位置:首頁 » 編程語言 » php資料庫連接

php資料庫連接

發布時間: 2022-01-08 18:18:24

php怎麼連接資料庫

1、資料庫連接第一步:配置mysql_connect()的參數
參數依次為:主機地址,用戶名,用戶密碼
2、mysql_pconnect()與mysql_connect()是不一樣的,pconnect顧名思義是持久連接
3、伺服器連接成功後,需要你選擇你需要用的資料庫
4、使用mydql_close()可以關閉資料庫連接資源,避免長時間佔用啟用資源消耗
5、mysqli_connect( )是mysql連接的另一種方式,參數形式一樣
6、首次使用mysql連接資料庫時,要記得使用輸入邏輯判斷,伺服器連接不成功或者選擇資料庫不成功,都要用Mysql_error或者mysql_errno來報錯
7、mysql的報錯,能夠幫助你准確地定位到錯誤發生在哪裡。

Ⅱ php怎麼連接mysql資料庫啊

mysql_connect('伺服器名','用戶名','密碼') or die('鏈接資料庫出錯'); //鏈接資料庫
mysql_select_db('資料庫名') or die('選擇資料庫失敗');; //選擇你想要兩件的資料庫

Ⅲ PHP連接資料庫

<?php

$Link=mysql_connect('localhost','root','123456')ordie('資料庫伺服器連接失敗,錯誤信息:'.mysql_error());

mysql_select_db('test')ordie('資料庫選取失敗,錯誤信息:'.mysql_error());


mysql_query('setnamesgbk');

?>
<?php

include_once'conn.php';//引入連接資料庫

if(!empty($_POST['sub'])){

$title=$_POST['title'];

$con=$_POST['con'];

$sql="insertinto`willie`(`title`,`dates`,`contents`)values('$title',now(),'$con')";
echomysql_query($sql)?'插入成功':'插入失敗,錯誤信息:'.mysql_error();}

Ⅳ 用php如何連接MySQL資料庫

php鏈接mysql必備條件:

  • 已安裝mysql資料庫;

  • 檢查php環境是否已開啟mysql擴展(一般情況下是開啟的);

    檢查方法:a.使用phpinfo();函數,看有沒有mysql項;b.打開php.ini文件,檢查php_mysql.dll前分號是否已取掉。

  • php鏈接代碼如下:

    <?php
    //設置編碼格式header("Content-type:text/html;charset=utf-8");//定義資料庫主機地址$host="localhost";//定義mysql資料庫登錄用戶名$user="root";//定義mysql資料庫登錄密碼$pwd="";//鏈接資料庫$conn=mysql_connect($host,$user,$pwd);//對連接進行判斷if(!$conn){die("資料庫連接失敗!".mysql_errno());}else{echo"資料庫連接成功!";}?>

Ⅳ php連接mysql

public function getInfo(){
$this->userName=$this->userInfo["name"];
$this->userPSW=$this->userInfo["password"];
$this->userAge=$this->userInfo["age"];
$this->userGrade=$this->userInfo["grade"];
}
你這里不對吧,應該是
$this->userName=$this->userInfo[0]["name"];
$this->userPSW=$this->userInfo[0]["password"];
$this->userAge=$this->userInfo[0]["age"];
$this->userGrade=$this->userInfo[0]["grade"];
就算你只取一條數據,也還是一個數組,所以要加下標0

Ⅵ 如何通過php實現mysql資料庫連接

php鏈接mysql必備條件:

  • 已安裝mysql資料庫;

  • 檢查php環境是否已開啟mysql擴展(一般情況下是開啟的);

    檢查方法:a.使用phpinfo();函數,看有沒有mysql項;b.打開php.ini文件,檢查php_mysql.dll前分號是否已取掉。

  • php鏈接代碼如下:

    4
    <?php//設置編碼格式header("Content-type:text/html;charset=utf-8");//定義資料庫主機地址$host="localhost";//定義mysql資料庫登錄用戶名$user="root";//定義mysql資料庫登錄密碼$pwd="";//鏈接資料庫$conn=mysql_connect($host,$user,$pwd);//對連接進行判斷if(!$conn){die("資料庫連接失敗!".mysql_errno());}else{echo"資料庫連接成功!";}?>

Ⅶ 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>
<!-- 為防止出現意外,請按照此標准順序書寫.其實也無所謂了 -->
<host>localhost</host>
<user>root</user>
<password>123456</password>
<db>test</db>
<port>3306</port></mysql>12345678910

解析

<?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>讀取資料庫配置文件信息出錯!</mark><br />" );
} return $dbconfig;
}
}

資料庫連接池

對於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 ( "<mark>utils.php文件丟失,無法進行配置文件的初始化操作!</mark><br />" );
}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>連接資料庫失敗!</mark><br />" );
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>資料庫連接池已滿</mark><br />" );
} else {
array_push ( $this->dbpool, $conn );
}
}
}

Ⅷ PHp如何連接資料庫

不知道你用的是什麼資料庫,如果是Mysql的話,一個php函數就搞定了
mysql_connect('host',
'username',
'password');
三個參數分別是
資料庫主機
ip,
資料庫用戶名,
資料庫密碼

Ⅸ php如何連接資料庫

$mysql_host="localhost";地址
$mysql_user="root";用戶名
$mysql_password="123";密碼
$mysql_database="001online";資料庫名
$conn=mysql_connect("$mysql_host","$mysql_user","$mysql_password");
if(!$conn){die("連接資料庫失敗:".mysql_error());}
mysql_select_db("$mysql_database",$conn);
mysql_query("setcharacterset'utf-8'");
mysql_query("setnames'utf-8'");

Ⅹ 怎麼將php與資料庫連接

php鏈接mysql必備條件:
已安裝mysql資料庫;

檢查php環境是否已開啟mysql擴展(一般情況下是開啟的);
檢查方法:a.使用phpinfo();函數,看有沒有mysql項;b.打開php.ini文件,檢查php_mysql.dll前分號是否已取掉。
php鏈接代碼如下:
<?php
//設置編碼格式
header("Content-type:text/html;charset=utf-8");

//定義資料庫主機地址
$host="localhost";

//定義mysql資料庫登錄用戶名
$user="root";

//定義mysql資料庫登錄密碼
$pwd="";

//鏈接資料庫
$conn = mysql_connect($host,$user,$pwd);

//對連接進行判斷
if(!$conn){
die("資料庫連接失敗!".mysql_errno());
}else{

echo "資料庫連接成功!";
}
?>

熱點內容
單片機android 發布:2024-09-20 09:07:24 瀏覽:765
如何提高三星a7安卓版本 發布:2024-09-20 08:42:35 瀏覽:664
如何更換伺服器網站 發布:2024-09-20 08:42:34 瀏覽:311
子彈演算法 發布:2024-09-20 08:41:55 瀏覽:289
手機版網易我的世界伺服器推薦 發布:2024-09-20 08:41:52 瀏覽:817
安卓x7怎麼邊打游戲邊看視頻 發布:2024-09-20 08:41:52 瀏覽:162
sql資料庫安全 發布:2024-09-20 08:31:32 瀏覽:94
蘋果連接id伺服器出錯是怎麼回事 發布:2024-09-20 08:01:07 瀏覽:507
編程鍵是什麼 發布:2024-09-20 07:52:47 瀏覽:658
學考密碼重置要求的證件是什麼 發布:2024-09-20 07:19:46 瀏覽:481