PHP寫MySQL資料 實現代碼_PHP教程

來源:互聯網
上載者:User
PHP向MySQL資料庫中寫入資料有三個步驟:
1,PHP和MySQL建立串連關係
2,開啟MySQL資料庫
3,接受頁面資料,PHP錄入到指定的表中
1、2兩步可直接使用一個資料庫連結檔案即可:conn.php
複製代碼 代碼如下:
mysql_connect("localhost","root","");//串連MySQL
mysql_select_db("hello");//選擇資料庫
?>

當然,前提是已經安裝WEB伺服器、PHP和MySQL,並且建立MySQL表“cnbruce”
mysql_connect()中三個參數分別為MySQL地址、MySQL使用者名稱和MySQL密碼
然後就是通過WEB頁面傳遞資料,讓PHP通過SQL語句將資料寫入MySQL資料庫指定的表中,比如建立檔案 post.php
複製代碼 代碼如下:
require_once("conn.php");//引用資料庫連結檔案
$uname = $_GET['n'];//GET方法為URL參數傳遞
$psw = $_GET['p'];
$psw=md5($psw);//直接使用MD5加密
$sql = "insert into members(username,password) values ('$uname','$psw')";
mysql_query($sql);//借SQL語句插入資料
mysql_close();//關閉MySQL串連
echo "成功錄入資料";
?>

測試頁面: http://localhost/post.php?n=cnbruce&p=i0514
即可向MySQL資料庫hello的members表中插入新的資料“cnbruce”到username欄位、“i0514”到password欄位

http://www.bkjia.com/PHPjc/320202.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/320202.htmlTechArticlePHP向MySQL資料庫中寫入資料有三個步驟: 1,PHP和MySQL建立串連關係 2,開啟MySQL資料庫 3,接受頁面資料,PHP錄入到指定的表中 1、2兩步可直...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.