php如何上傳數(shù)據(jù)庫中,php怎么從數(shù)據(jù)庫讀取數(shù)據(jù)

怎樣用php實現(xiàn)上傳圖片到數(shù)據(jù)庫

php實現(xiàn)上傳圖片保存到數(shù)據(jù)庫的方法。具體分析如下:

桂林網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,桂林網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為桂林近1000家提供企業(yè)網(wǎng)站建設(shè)服務。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務好的桂林做網(wǎng)站的公司定做!

php 上傳圖片,一般都使用move_uploaded_file方法保存在服務器上。但如果一個網(wǎng)站有多臺服務器,就需要把圖片發(fā)布到所有的服務器上才能正常使用(使用圖片服務器的除外)

如果把圖片數(shù)據(jù)保存到數(shù)據(jù)庫中,多臺服務器間可以實現(xiàn)文件共享,節(jié)省空間。

首先圖片文件是二進制數(shù)據(jù),所以需要把二進制數(shù)據(jù)保存在mysql數(shù)據(jù)庫。

mysql數(shù)據(jù)庫提供了BLOB類型用于存儲大量數(shù)據(jù),BLOB是一個二進制對象,能容納不同大小的數(shù)據(jù)。

BLOB類型有以下四種,除存儲的最大信息量不同外,其他都是一樣的??筛鶕?jù)需要使用不同的類型。

TinyBlob?????? 最大 255B

Blob????????????? 最大 65K

MediumBlob? 最大 16M

LongBlob????? 最大 4G

數(shù)據(jù)表photo,用于保存圖片數(shù)據(jù),結(jié)構(gòu)如下:

CREATE?TABLE?`photo`?(??

`id`?int(10)?unsigned?NOT?NULL?auto_increment,??

`type`?varchar(100)?NOT?NULL,??

`binarydata`?mediumblob?NOT?NULL,??

PRIMARY?KEY??(`id`)??

)?ENGINE=MyISAM?DEFAULT?CHARSET=latin1?AUTO_INCREMENT=1?;

upload_image_todb.php代碼如下:

?php??

//?連接數(shù)據(jù)庫??

$conn=@mysql_connect("localhost","root","")??or?die(mysql_error());??

@mysql_select_db('demo',$conn)?or?die(mysql_error());?//?判斷action??

$action?=?isset($_REQUEST['action'])??$_REQUEST['action']?:?'';?

//?上傳圖片??

if($action=='add'){??

$image?=?mysql_escape_string(file_get_contents($_FILES['photo']['tmp_name']));??

$type?=?$_FILES['photo']['type'];??

$sqlstr?=?"insert?into?photo(type,binarydata)?values('".$type."','".$image."')";??

@mysql_query($sqlstr)?or?die(mysql_error());??

header('location:upload_image_todb.php');??

exit();??

//?顯示圖片??

}elseif($action=='show'){??

$id?=?isset($_GET['id'])??intval($_GET['id'])?:?0;??

$sqlstr?=?"select?*?from?photo?where?id=$id";??

$query?=?mysql_query($sqlstr)?or?die(mysql_error());??

$thread?=?mysql_fetch_assoc($query);??

if($thread){??

header('content-type:'.$thread['type']);??

echo?$thread['binarydata'];??

exit();??

}??

}else{??

//?顯示圖片列表及上傳表單??

???

!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?""??

html??

head??

meta?http-equiv="content-type"?content="text/html;?charset=utf-8"??

title?upload?image?to?db?demo?/title??

/head??

body??

form?name="form1"?method="post"?action="upload_image_todb.php"?enctype="multipart/form-data"??

p圖片:input?type="file"?name="photo"/p??

pinput?type="hidden"?name="action"?value="add"input?type="submit"?name="b1"?value="提交"/p??

/form??

?php??

$sqlstr?=?"select?*?from?photo?order?by?id?desc";??

$query?=?mysql_query($sqlstr)?or?die(mysql_error());??

$result?=?array();??

while($thread=mysql_fetch_assoc($query)){??

$result[]?=?$thread;??

}??

foreach($result?as?$val){??

echo?'pimg?

src="upload_image_todb.php?action=showid='.$val['id'].'t='.time().'"

width="150"/p';??

}??

???

/body??

/html??

?php??

}??

?

程序運行截圖和數(shù)據(jù)庫截圖:

php怎么把數(shù)據(jù)導入數(shù)據(jù)庫

需要PHP基礎(chǔ)知識和數(shù)據(jù)庫基礎(chǔ)知識。

以SQL為例。使用PHP MySQL 函數(shù)可以編輯數(shù)據(jù)庫。

mysql_connect() 函數(shù)打開MySQL 連接。舉例

?php

$con = mysql_connect("localhost","mysql_user","mysql_pwd");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}// 一些代碼...mysql_close($con);

?

mysql_connect()三個參數(shù)分別是服務器名,連接賬號,連接密碼。

連接之后,可以使用mysql_select_db()設(shè)置要處理的數(shù)據(jù)庫,后面則是用數(shù)據(jù)庫語句處理數(shù)據(jù)。SQL語法簡介網(wǎng)頁鏈接

php怎么把表單提交的數(shù)據(jù)放到數(shù)據(jù)庫中。

php數(shù)據(jù)庫操作主要分為5個步驟:1連接MYSQL

2連接到你的數(shù)據(jù)庫

3寫SQL語句

4運行sql語句

5關(guān)閉數(shù)據(jù)庫

//第一步

$con

=

mysql_connect("localhost","root","123456789");

//第二步

mysql_select_db('rankingme',$conn);

//第三步

$sql="insert

into

lili

(name,sex,et,hobby,photo,tel,address,content,time)

values

($name,$sex,$et,$hobby,$photo,$tel,$address,$content,$time)"

//第四步

mysql_query($sql);

//第五步

mysql_close($con);

網(wǎng)站題目:php如何上傳數(shù)據(jù)庫中,php怎么從數(shù)據(jù)庫讀取數(shù)據(jù)
鏈接URL:http://muchs.cn/article46/hcgshg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)用戶體驗、網(wǎng)站收錄、商城網(wǎng)站手機網(wǎng)站建設(shè)、搜索引擎優(yōu)化

廣告

聲明:本網(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)

搜索引擎優(yōu)化