建站學院整理了一些MYSQL的技巧與實用文章,陸續(xù)發(fā)給大家,本篇文章主要介紹任何數(shù)據(jù)存入mysql,及從中提取的程序的實例PHP代碼,如下:
<?php // 文件名:filedb.php 將文件儲存到數(shù)據(jù)庫中 /* 將文件上傳存入 MySQL 數(shù)據(jù)庫的例子 數(shù)據(jù)表的結構如下: CREATE TABLE files ( id int(4) NOT NULL auto_increment, filename varchar(64), filesize int(16), filetype varchar(64), filecontent longblob, remark text, PRIMARY KEY (id) ); */ // 設定系統(tǒng)參數(shù)變量,根據(jù)需要修改 $DB_SERVER = "127.0.0.1"; # 數(shù)據(jù)庫連接字 $DB_USER = "root"; # 用戶名 $DB_PASS = ""; # 密碼 $DB_NAME = "article"; # 數(shù)據(jù)庫名 $TABLE_NAME = "files"; # 數(shù)據(jù)表名 $HANDLER_SCRIPT = "mysql數(shù)據(jù)庫顯示文件.php"; # 處理數(shù)據(jù)的腳本文件名 $PHP_SELF=$_SERVER['PHP_SELF']; ?> <html> <head> <title>將文件存入數(shù)據(jù)庫</title> <style> caption { background-color: #E6F4FF; font-size:14px} td { background-color: #E6F4FF; font-size:12px} th { background-color: #FFCC00; font-size:12px} div {font-size:12px} </style> </head> <body> <form enctype='multipart/form-data' method='post'> <table cellspacing=1 cellpadding=5 > <caption>文件存入數(shù)據(jù)庫管理器</caption> <input type='hidden' name='MAX_FILE_SIZE' value=10489760> <tr> <td>上傳文件:</td> <td><input type='file' name='userfile'></td> </tr> <tr valign=top> <td>文件說明:</td> <td><textarea name='description' rows='4' cols='40'></textarea></td> </tr> <tr> <td colspan=2 align=center><input type='submit' name='submit' value=' 上傳 '> </tr> </table> </form> <?php if (isset($_POST["submit"])) // 表單被提交后執(zhí)行以下代碼 { if (!is_uploaded_file($_FILES['userfile']['tmp_name'])) // 檢查文件是否上傳成功 { die("文件上傳失敗!"); } $link = mysql_connect($DB_SERVER, $DB_USER, $DB_PASS) // 連接數(shù)據(jù)庫 or die("連接數(shù)據(jù)庫失敗!"); mysql_select_db($DB_NAME); $filename = addslashes($_FILES['userfile']['name']); # 為了正確保存入數(shù)據(jù)庫, $filesize = addslashes($_FILES['userfile']['size']); # 對所有的插入數(shù)據(jù)都要 $filetype = addslashes($_FILES['userfile']['type']); # 進行 addslashes 操作 // 讀入上傳的文件并做預處理 # $tmp_name = $_FILES['userfile']['tmp_name']; $fd = fopen ($tmp_name, "rb"); $contents = fread ($fd, filesize ($tmp_name)); fclose ($fd); $filecontent = addslashes($contents); $tmp_name = $_FILES['userfile']['tmp_name']; $fd = fopen ($tmp_name, "rb"); $contents = fread ($fd, filesize ($tmp_name)); fclose ($fd); $filecontent = addslashes($contents); $remark = addslashes($_POST['description']); $query_string = "INSERT INTO $TABLE_NAME VALUES ('', '$filename', '$filesize', '$filetype', '$filecontent', '$remark')"; $result = mysql_query($query_string) or die("數(shù)據(jù)插入失敗!"); # 進行數(shù)據(jù)插入操作 echo "<div id='info'>保存成功!<br>"; echo "文件名 :{$_FILES['userfile']['name']}<br>"; echo "文件大小:{$_FILES['userfile']['size']} 字節(jié)<br>"; echo "文件類型:{$_FILES['userfile']['type']}</div>"; echo "<input type="submit" value=刷新 onclick="window.location='$PHP_SELF'">"; } echo " <table border=2 bordercolor=#055AA0 cellspacing=0 width=840 cellpadding=0 style="border-collapse: collapse ;LEFT: 0px; WORD-WRAP: break-word; word-break:break-all" > <tr> <th width=>ID</th> <th>文件名</th> <th>文件大小</th> <th>文件類型</th> <th>說明</th> <th>連接</th> <th>刪除</th> </tr>"; $link = mysql_connect($DB_SERVER, $DB_USER, $DB_PASS) // 顯示數(shù)據(jù)庫已有記錄 or die("連接數(shù)據(jù)庫失敗!"); mysql_select_db($DB_NAME); $query_string = "SELECT * FROM $TABLE_NAME ORDER BY id"; # 取出所有記錄,無分頁顯示功能 $result = mysql_query($query_string) or die("查詢出錯!"); while ($row = mysql_fetch_array($result)) { $remark = nl2br(htmlspecialchars($row["remark"])); # 為了正確顯示說明,需要作預處理 echo "<tr> <td>$row[id]</td> <td>$row[filename]</td> <td align=right>$row[filesize]</td> <td>$row[filetype]</td> <td>$remark</td> <td nowrap><a href='$HANDLER_SCRIPT?action=show&id=$row[id]' target=_blank>點擊查看</td> <td nowrap><a href='#' OnClick='window.open("$HANDLER_SCRIPT?action=del&id=$row[id]");location.href="$_SERVER[PHP_SELF]";'>點擊刪除</td> </tr>"; } echo "</table>"; ?> </body> </html> |