1、用 mysql_connect 的方法,PHP7會(huì)報(bào)致命錯(cuò)誤
我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、成都做網(wǎng)站、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、弋陽ssl等。為1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的弋陽網(wǎng)站制作公司
$conn= mysql_connect('localhost','xueyanxiang','xueyanxiang');
Fatal error : Uncaught Error: Call to undefined function mysql_connect() in /Users/xueyanxiang/work/test/xue.php:31 Stack trace: #0 /Users/xueyanxiang/work/test/xue.php(119): xue-run() #1 {main} thrown in? /Users/xueyanxiang/work/test/xue.php ?on line? 31
原因是:
PHP5中使用mysql_connect()函數(shù)進(jìn)行連接,但實(shí)際上,PHP5.5開始,MySQL就不推薦使用了,屬于廢棄函數(shù)
PHP7中貌似已經(jīng)徹底不支持了,根據(jù)官網(wǎng)說明,取而代之的是如下兩個(gè):
本擴(kuò)展自 PHP 5.5.0 起已廢棄,并在將來會(huì)被移除。應(yīng)使用?MySQLi?或?PDO_MySQL?擴(kuò)展來替換之。參見?MySQL:選擇
API?指南以及相關(guān) FAQ?以獲取更多信息。用以替代本函數(shù)的有:
mysqli_connect()
PDO::__construct()
使用時(shí),不要在使用mysql_connect了,可以換用mysqli_connect(),用法基本類似吧,據(jù)說是面向?qū)ο蟮膸臁?/p>
php.ini中,也只有extension=php_mysqli.dll,而不再有extension=php_mysql.dll這個(gè)拓展了。
2、可以使用mysqli,對(duì)象化,方法名與被廢棄的類似
$conn= mysqli_connect('localhost','xueyanxiang','xueyanxiang');
3、PDO工具,推薦使用
$dbh= "mysql:host=localhost;dbname=test";
$db= new PDO($dbh,'xueyanxiang','xueyanxiang');
$objQuery= $db-query("select * from user;");
$res= $objQuery-fetchAll(PDO::FETCH_ASSOC);
不填寫參數(shù)的話,默認(rèn)是關(guān)聯(lián)和索引都有,如下圖
常規(guī)方式
常規(guī)方式就是按部就班的讀取文件了。其余的話和上述方案一致。
// 讀取配置文件內(nèi)容
$handle = fopen("filepath", "r"); ? ? ? ? ? ?$content = fread($handle, filesize("filepath"));123
PHP解析XML
上述兩種讀取文件,其實(shí)都是為了PHP解析XML來做準(zhǔn)備的。關(guān)于PHP解析XML的方式的博客有很多。方式也有很多,像simplexml,XMLReader,DOM啦等等。但是對(duì)于比較小型的xml配置文件,simplexml就足夠了。
配置文件
?xml version="1.0" encoding="UTF-8" ?mysql
!-- 為防止出現(xiàn)意外,請(qǐng)按照此標(biāo)準(zhǔn)順序書寫.其實(shí)也無所謂了 --
hostlocalhost/host
userroot/user
password123456/password
dbtest/db
port3306/port/mysql12345678910
解析
?php/**
* 作為解析XML配置文件必備工具
*/class XMLUtil {
public static $dbconfigpath = "./db.config.xml"; ? ?public static function getDBConfiguration() {
$dbconfig = array (); ? ? ? ?try { ? ? ? ? ? ?// 讀取配置文件內(nèi)容
$handle = fopen(self::$dbconfigpath, "r"); ? ? ? ? ? ?$content = fread($handle, filesize(self::$dbconfigpath)); ? ? ? ? ? ?// 獲取xml文檔根節(jié)點(diǎn),進(jìn)而獲取相關(guān)的數(shù)據(jù)庫信息
$mysql = simplexml_load_string($content); ? ? ? ? ? ?// 將獲取到的xml節(jié)點(diǎn)信息賦值給關(guān)聯(lián)數(shù)組,方便接下來的方法調(diào)用
$dbconfig['host'] = $mysql-host; ? ? ? ? ? ?$dbconfig['user'] = $mysql-user; ? ? ? ? ? ?$dbconfig['password'] = $mysql-password; ? ? ? ? ? ?$dbconfig['db'] = $mysql-db; ? ? ? ? ? ?$dbconfig['port'] = $mysql-port; ? ? ? ? ? ?// 將配置信息以關(guān)聯(lián)數(shù)組的形式返回
return $dbconfig;
} catch ( Exception $e ) { ? ? ? ? ? ?throw new RuntimeException ( "mark讀取數(shù)據(jù)庫配置文件信息出錯(cuò)!/markbr /" );
} ? ? ? ?return $dbconfig;
}
}1234567891011121314151617181920212223242526272829
數(shù)據(jù)庫連接池
對(duì)于PHP程序而言,優(yōu)化永無止境。而數(shù)據(jù)庫連接池就在一定程度上起到了優(yōu)化的作用。其使得對(duì)用戶的每一個(gè)請(qǐng)求而言,無需每次都像數(shù)據(jù)庫申請(qǐng)鏈接資源。而是通過已存在的數(shù)據(jù)庫連接池中的鏈接來返回,從時(shí)間上,效率上,都是一個(gè)大大的提升。
于是,這里簡(jiǎn)單的模擬了一下數(shù)據(jù)庫連接池的實(shí)現(xiàn)。核心在于維護(hù)一個(gè)“池”。
從池子中取,用畢,歸還給池子。
?php/**x
* ?PHP中的數(shù)據(jù)庫 工具類設(shè)計(jì)
* ?郭璞
* ?2016年12月23日
*
**/class DbHelper { ? ?private $dbconfig; ? ?private $dbpool; ? ?public $poolsize; ? ?public function __construct($poolsize = 20) { ? ? ? ?if (! file_exists ( "./utils.php" )) { ? ? ? ? ? ?throw new RuntimeException ( "markutils.php文件丟失,無法進(jìn)行配置文件的初始化操作!/markbr /" );
}else {
require './utils.php';
} ? ? ? ?// 初始化 配置文件信息
$this-dbconfig = XMLUtil::getDBConfiguration (); ? ? ? ?// 準(zhǔn)備好數(shù)據(jù)庫連接池“偽隊(duì)列”
$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連接數(shù)據(jù)庫失?。?markbr /" );
array_push ( $this-dbpool, $conn );
}
} ? ?/**
* 從數(shù)據(jù)庫連接池中獲取一個(gè)數(shù)據(jù)庫鏈接資源
*
* @throws ErrorException
* @return mixed
*/
public function getConn() { ? ? ? ?if (count ( $this-dbpool ) = 0) { ? ? ? ? ? ?throw new ErrorException ( "mark數(shù)據(jù)庫連接池中已無鏈接資源,請(qǐng)稍后重試!/mark" );
} else { ? ? ? ? ? ?return array_pop ( $this-dbpool );
}
} ? ?/**
* 將用完的數(shù)據(jù)庫鏈接資源放回到數(shù)據(jù)庫連接池
*
* @param unknown $conn
* @throws ErrorException
*/
public function release($conn) { ? ? ? ?if (count ( $this-dbpool ) = $this-poolsize) { ? ? ? ? ? ?throw new ErrorException ( "mark數(shù)據(jù)庫連接池已滿/markbr /" );
} else {
array_push ( $this-dbpool, $conn );
}
}
}
首先把數(shù)據(jù)庫路徑改一下試試:
realpath("/Shujuku.mdb") 改為 realpath("shujuku.mdb")
如果還是不行,可以改為ODBC連接方式再試試:
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=". realpath("shujuku.mdb");
PHP連接mysql數(shù)據(jù)庫是PHP新手們必須要掌握的一項(xiàng)技能,只要掌握了PHP對(duì)數(shù)據(jù)庫進(jìn)行增刪改查等操作,就可以寫出一些簡(jiǎn)單且常見的程序。如留言表,新聞頁等。本篇文章主要給大家詳細(xì)介紹PHP連接Mysql數(shù)據(jù)庫的兩種常用方法。
下面我們通過具體的代碼示例來給大家詳細(xì)介紹兩種PHP連接mysql數(shù)據(jù)庫的方法。
mysqli連接數(shù)據(jù)庫和pdo連接數(shù)據(jù)庫。
第一種方法:使用mysqli連接mysql數(shù)據(jù)庫
代碼實(shí)例如下:
?php
$host='127.0.0.1';
$user='root';
$password='root';
$dbName='php';
$link=new mysqli($host,$user,$password,$dbName);
if ($link-connect_error){
die("連接失?。?.$link-connect_error);
}
$sql="select * from admins";
$res=$link-query($sql);
$data=$res-fetch_all();
var_dump($data);
在經(jīng)過一系列的連接操作后,我們?cè)賱?chuàng)建一個(gè)sql語句對(duì)其中數(shù)據(jù)表進(jìn)行查詢檢驗(yàn)。在上述代碼中,我們要先創(chuàng)建一些需要用到的變量,如數(shù)據(jù)庫用戶名、數(shù)據(jù)庫名密碼等。然后我們用面向?qū)ο蟮姆绞竭B接了名為php的數(shù)據(jù)庫。再通過if條件語句,connect-error方法判斷PHP連接數(shù)據(jù)庫是否成功。
這里我們先登錄phpmyadmin看看是否存在php數(shù)據(jù)庫,從下圖可以知道是存在php這個(gè)數(shù)據(jù)庫的。
最后通過瀏覽器訪問,結(jié)果如下圖:
從圖中可以得知,我們成功地連接了php數(shù)據(jù)庫,并且能查詢出數(shù)據(jù)表信息。
第二種方法:使用PDO連接數(shù)據(jù)庫
代碼示例如下:
?php
$host='127.0.0.1';
$user='root';
$password='root';
$dbName='php';
$pdo=new PDO("mysql:host=$host;dbname=$dbName",$user,$password);
$sql="select * from admins";
$data=$pdo-query($sql)-fetch();
var_dump($data);
PHP連接Mysql步驟以上就是關(guān)于PHP連接數(shù)據(jù)庫查詢數(shù)據(jù)的兩種常用方法詳解,更多相關(guān)教程請(qǐng)?jiān)L問php中文網(wǎng)mysql視頻教程,歡迎參考學(xué)習(xí)
安全。php連接數(shù)據(jù)庫有三層密碼層層防護(hù),源碼很安全。數(shù)據(jù)庫是“按照數(shù)據(jù)結(jié)構(gòu)來組織、存儲(chǔ)和管理數(shù)據(jù)的倉庫”,是一個(gè)長期存儲(chǔ)在計(jì)算機(jī)內(nèi)的集合。
直接寫代碼啊。
我寫了一遍截圖看。第一行參數(shù)主機(jī)、用戶名、密碼;第二行選擇數(shù)據(jù)庫‘第三行選擇字符集’
你自己試下