數(shù)據(jù)庫(kù)有很多種類(lèi):mysql,oracle,mssql,db2等等。PHP操作數(shù)據(jù)庫(kù)的時(shí)候,要保證該類(lèi)型數(shù)據(jù)庫(kù)的擴(kuò)展已開(kāi)啟。這里連接的數(shù)據(jù)庫(kù)以mysql為例:
創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括西豐網(wǎng)站建設(shè)、西豐網(wǎng)站制作、西豐網(wǎng)頁(yè)制作以及西豐網(wǎng)絡(luò)營(yíng)銷(xiāo)策劃等。多年來(lái),我們專(zhuān)注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,西豐網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶(hù)以成都為中心已經(jīng)輻射到西豐省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶(hù)的支持與信任!
?php
//數(shù)據(jù)庫(kù)服務(wù)器地址
$host="localhost";?
//連接數(shù)據(jù)庫(kù)用戶(hù)名
$uname="root";?
//連接數(shù)據(jù)庫(kù)密碼
$upass="";?
//連接數(shù)據(jù)庫(kù)
$conn=mysql_connect($host,?$uname,$upass);
//判斷連接
if(!$conn){
die("連接數(shù)據(jù)庫(kù)失??!").mysql_errno();????
}
//連接成功,其他操作省略
?
下面是一個(gè)php連接數(shù)據(jù)庫(kù)操作的測(cè)試代碼,你可以參考:
?php
$id = mysql_connect("localhost", "root", "123456") or die(mysql_error());
$ok = mysql_select_db("zf2", $id) or die(mysql_error());
if ($ok) {
echo "ok";
} else {
echo "no";
}
$rs = mysql_query("select * from album order by artist asc");
if ($rs) {
echo "sdfasf";
} else {
echo "fail";
}
if (mysql_num_rows($rs) != 0) {
while($row = mysql_fetch_array($rs)) {
print_r($row['id'] . "br");
}
}
unset($row);
mysql_free_result($rs);
mysql_close($id);
我也剛剛學(xué)PHP,正在研究中,雖然你只給10分........
首先,將代碼保存到一個(gè)文件,如:mysql.class.php
其次,在一個(gè)常用的文件里調(diào)用:比如頭部文件header.php,因?yàn)槲曳旁诟夸浰杂孟旅娣绞綄?dǎo)入其他文件:
require dirname(__FILE__) . 'include/config.php';
//導(dǎo)入類(lèi)文件
require dirname(__FILE__) . 'include/mysql.class.php';
//定義一個(gè)類(lèi)及初始化數(shù)據(jù)庫(kù)類(lèi)
$db = new mysql($db_host, $db_user, $db_pass, $db_name);
$db_host = $db_user = $db_pass = $db_name = NULL;
然后,在test.php文件調(diào)用:
require_once dirname(__FILE__) . '/header.php';
使用方法:
$sql = "讀取表";
$res = $db-query($sql);
$info = array();//定義數(shù)組
while($row=$db-fetchRow($res))
{
$arr['id'] = $row['id'];
$arr['title'] = $row['title'];
$info[] = $arr;
}
可以在顯示的地方用:
foreach($info as $i)
{
echo $i['title']."br /";
}
或是直接使用while
還用另一種調(diào)用方式:
$here_area = $db-getRow("select areaid,areaname from {$table}area where areaid='$areaid'");
$here[] = array('name'=$here_area['areaname'],'id'=$here_area['areaid']);
測(cè)試通過(guò),因?yàn)槲艺谑褂?....................................
config.php代碼:
?php
$db_host = "localhost";
$db_name = "test";
$db_user = "root";
$db_pass = "";
$table = "mini_";
$charset = "gb2312";
$dbcharset = "gbk";
?
mysql.class.php代碼:
?php
class mysql
{
var $link = NULL;
//自動(dòng)執(zhí)行__construct php5類(lèi)構(gòu)建方法,如果PHP4和PHP5同時(shí)使用會(huì)自動(dòng)使用PHP5的方法
function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $quiet = 0)
{
//自動(dòng)執(zhí)行時(shí)調(diào)用mysql函數(shù)
$this-mysql($dbhost, $dbuser, $dbpw, $dbname, $pconnect, $quiet);
}
//php4類(lèi)構(gòu)建方法,如果沒(méi)有 __construct 就自動(dòng)執(zhí)行此功能
function mysql($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $quiet = 0)
{
if ($quiet)
{
$this-connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, $quiet);
}
else
{
$this-settings = array(
'dbhost' = $dbhost,
'dbuser' = $dbuser,
'dbpw' = $dbpw,
'dbname' = $dbname,
'charset' = $charset,
'pconnect' = $pconnect
);
}
}
function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $quiet = 0)
{
global $dbcharset;
if ($pconnect)
{
if (!($this-link = @mysql_pconnect($dbhost, $dbuser, $dbpw)))
{
if (!$quiet)
{
$this-ErrorMsg("Can't pConnect MySQL Server($dbhost)!");
}
return false;
}
}
else
{
if (PHP_VERSION = '4.2')
{
$this-link = @mysql_connect($dbhost, $dbuser, $dbpw, true);
}
else
{
$this-link = @mysql_connect($dbhost, $dbuser, $dbpw);
mt_srand((double)microtime() * 1000000);
}
if (!$this-link)
{
if (!$quiet)
{
$this-ErrorMsg("Can't Connect MySQL Server($dbhost)!");
}
return false;
}
}
$this-dbhash = md5($this-root_path . $dbhost . $dbuser . $dbpw . $dbname);
$this-version = mysql_get_server_info($this-link);
if ($this-version '4.1')
{
if ($dbcharset != 'latin1')
{
mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary", $this-link);
}
if ($this-version '5.0.1')
{
mysql_query("SET sql_mode=''", $this-link);
}
}
if ($dbname)
{
if (mysql_select_db($dbname, $this-link) === false )
{
if (!$quiet)
{
$this-ErrorMsg("Can't select MySQL database($dbname)!");
}
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
function query($sql, $type = '')
{
if ($this-link === NULL)
{
$this-connect($this-settings['dbhost'], $this-settings['dbuser'], $this-settings['dbpw'], $this-settings['dbname'], $this-settings['charset'], $this-settings['pconnect']);
$this-settings = array();
}
if ($this-queryCount++ = 99)
{
$this-queryLog[] = $sql;
}
if ($this-queryTime == '')
{
if (PHP_VERSION = '5.0.0')
{
$this-queryTime = microtime(true);
}
else
{
$this-queryTime = microtime();
}
}
if (!($query = mysql_query($sql, $this-link)) $type != 'SILENT')
{
$this-error_message[]['message'] = 'MySQL Query Error';
$this-error_message[]['sql'] = $sql;
$this-error_message[]['error'] = mysql_error($this-link);
$this-error_message[]['errno'] = mysql_errno($this-link);
$this-ErrorMsg();
return false;
}
return $query;
}
function affected_rows()
{
return mysql_affected_rows($this-link);
}
function num_fields($query)
{
return mysql_num_fields($query);
}
function error()
{
return mysql_error($this-link);
}
function errno()
{
return mysql_errno($this-link);
}
function num_rows($query)
{
return mysql_num_rows($query);
}
function insert_id()
{
return mysql_insert_id($this-link);
}
function fetchRow($query)
{
return mysql_fetch_assoc($query);
}
function fetcharray($query)
{
return mysql_fetch_array($query);
}
function version()
{
return $this-version;
}
function close()
{
return mysql_close($this-link);
}
function ErrorMsg($message = '', $sql = '')
{
if ($message)
{
echo "$message\n\n";
}
else
{
echo "bMySQL server error report:";
print_r($this-error_message);
}
exit;
}
function getCol($sql)
{
$res = $this-query($sql);
if ($res !== false)
{
$arr = array();
while ($row = mysql_fetch_row($res))
{
$arr[] = $row[0];
}
return $arr;
}
else
{
return false;
}
}
function getOne($sql, $limited = false)
{
if ($limited == true)
{
$sql = trim($sql . ' LIMIT 1');
}
$res = $this-query($sql);
if ($res !== false)
{
$row = mysql_fetch_row($res);
if ($row !== false)
{
return $row[0];
}
else
{
return '';
}
}
else
{
return false;
}
}
function getAll($sql)
{
$res = $this-query($sql);
if ($res !== false)
{
$arr = array();
while ($row = mysql_fetch_assoc($res))
{
$arr[] = $row;
}
return $arr;
}
else
{
return false;
}
}
//使用: getRow($sql,true) 如果有true那值是 limit 1,讀取一條信息
function getRow($sql, $limited = false)
{
if ($limited == true)
{
$sql = trim($sql . ' LIMIT 1');
}
$res = $this-query($sql);
if ($res !== false)
{
return mysql_fetch_assoc($res);
}
else
{
return false;
}
}
}
?
根據(jù)你貼出來(lái)的意思是沒(méi)有定義這個(gè)函數(shù)mysql_pconnect()
估計(jì)你是調(diào)用了一個(gè)mysql操作類(lèi)的程序來(lái)操作數(shù)據(jù)庫(kù),
第一,看看你的mysql_pconnect函數(shù)名稱(chēng)是否寫(xiě)錯(cuò)
第二,如果是調(diào)用了類(lèi)里面的函數(shù),看看你有沒(méi)有重新創(chuàng)建類(lèi)
例如:$doc
=
new
DB_Doc_Read();//new
一個(gè)類(lèi)
$doc-mysql_pconnect();//調(diào)用類(lèi)中的函數(shù)
第三,如果沒(méi)有調(diào)用類(lèi),是直接寫(xiě)的函數(shù),那應(yīng)該是mysql_connect函數(shù)
圖片驗(yàn)證碼一般跟你的系統(tǒng)沒(méi)有關(guān)系,因?yàn)槭秋@示在客戶(hù)端的,把你代碼貼出來(lái)我看看。
ThinkPHP的學(xué)習(xí),連接操作數(shù)據(jù)庫(kù)是最關(guān)鍵的,這里就講一下ThinkPHP如何連接MySQL數(shù)據(jù)庫(kù)
方法/步驟
配置PHP5支持MySQL
打開(kāi)PHP配置文件“F:\PHP\php5328Win32\php.ini”,查找“extension=php_mysql.dll”,
把前面的分號(hào)去掉,保存文件,啟動(dòng)服務(wù);
卻提示“PHP startup: Unable to load dynamic library :F:\PHP\php_mysql.dll”錯(cuò)誤,
解決辦法,打開(kāi)PHP配置文件“F:\PHP\php5328Win32\php.ini”,查找“extension_dir = "ext"”,
把前面的分號(hào)去掉,保存文件,啟動(dòng)服務(wù),問(wèn)題解決;
準(zhǔn)備測(cè)試數(shù)據(jù)
配置數(shù)據(jù)庫(kù)連接信息
打開(kāi)配置文件“F:\PHP\Application\Common\Conf\config.php”,文件內(nèi)容如下:
添加一個(gè)操作數(shù)據(jù)的方法
打開(kāi)上一篇新建的控制器文件“F:\PHP\Application\Index\Controller\IndexController.class.php”,添加一個(gè)函數(shù),代碼如下:測(cè)試一下吧
如果PHP5配置不正確,可能出現(xiàn)以下錯(cuò)誤信息:
新聞標(biāo)題:php5連接數(shù)據(jù)庫(kù)代碼,PHP連接數(shù)據(jù)庫(kù)代碼
當(dāng)前地址:http://aaarwkj.com/article30/dssjepo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、關(guān)鍵詞優(yōu)化、網(wǎng)站策劃、Google、App設(shè)計(jì)、動(dòng)態(tài)網(wǎng)站
聲明:本網(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)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)