本文實例講述了PHP實現(xiàn)的pdo連接數(shù)據(jù)庫并插入數(shù)據(jù)功能。分享給大家供大家參考,具體如下:
10年積累的成都網站設計、成都做網站經驗,可以快速應對客戶對網站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網絡服務。我雖然不認識你,你也不認識我。但先網站制作后付款的網站建設流程,更有昌江免費網站建設讓你可以放心的選擇與我們合作。
創(chuàng)建配置文件
pdo_config.php
?php
$db_Type
=
"mysql";//數(shù)據(jù)庫類型
$host
=
"localhost";//主機名
$dbName
=
"test";//數(shù)據(jù)庫名
$userName
=
"root";//用戶名
$password
=
"root";//密碼
$dsn
=
"{$db_Type}:host={$host};dbname={$dbName}";
?
pdo插入數(shù)據(jù)庫
pdo_insert.php
?php
header('Content-type:text/html;
charset=utf-8');
require
'pdo_config.php';
try{
$pdo
=
new
PDO
($dsn,$userName,$password);//創(chuàng)建一個連接對象
$pdo-exec('set
names
utf8');//設置編碼
$sql
=
"INSERT
student
(name,email)
VALUES
('李四','123@qq.com')";
$pdo-exec($sql);
}catch
(PDOException
$e){
die('操作失敗'.$e-getMessage());
}
//關閉連接
$pdo
=
null;
?
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫技巧總結》、《php+mysqli數(shù)據(jù)庫程序設計技巧總結》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
您可能感興趣的文章:關于php連接mssql:pdo
odbc
sql
serverPHP5中使用PDO連接數(shù)據(jù)庫的方法PHP中PDO連接數(shù)據(jù)庫中各種DNS設置方法小結ThinkPHP框架基于PDO方式連接數(shù)據(jù)庫操作示例PHP使用ODBC連接數(shù)據(jù)庫的方法tp5(thinkPHP5)框架連接數(shù)據(jù)庫的方法示例PHP7使用ODBC連接SQL
Server2008
R2數(shù)據(jù)庫示例【基于thinkPHP5.1框架】tp5(thinkPHP5)操作mongoDB數(shù)據(jù)庫的方法thinkPHP5實現(xiàn)數(shù)據(jù)庫添加內容的方法tp5(thinkPHP5)框架數(shù)據(jù)庫Db增刪改查常見操作總結PHP利用pdo_odbc實現(xiàn)連接數(shù)據(jù)庫示例【基于ThinkPHP5.1搭建的項目】
$conn
=
@mysql_connect("localhost","用戶名","密碼");
if
(!$conn){
die("連接數(shù)據(jù)庫失?。?
.
mysql_error());
}
確認沒輸出DIE里的內容,那就是連接成功了
mysql_select_db("數(shù)據(jù)庫名(不是表明哦)",
$conn);
$sql
=
“SQL語句";
SQL插入例子:INSERT
INTO
user(字段名,ziduan2)VALUES(值,值2)
if(!mysql_query($sql,$conn)){
echo
"添加數(shù)據(jù)失?。?.mysql_error();
}
else
{
echo
"添加數(shù)據(jù)成功!";
}
把來自表單的數(shù)據(jù)插入數(shù)據(jù)庫
現(xiàn)在,我們創(chuàng)建一個 HTML 表單,這個表單可把新記錄插入 "Persons" 表。
這是這個 HTML 表單:
html
body
form?action="insert.php"?method="post"
Firstname:?input?type="text"?name="firstname"?/
Lastname:?input?type="text"?name="lastname"?/
Age:?input?type="text"?name="age"?/
input?type="submit"?/
/form
/body
/html
當用戶點擊上例中 HTML 表單中的提交按鈕時,表單數(shù)據(jù)被發(fā)送到 "insert.php"。"insert.php" 文件連接數(shù)據(jù)庫,并通過 $_POST 變量從表單取回值。然后,mysql_query() 函數(shù)執(zhí)行 INSERT INTO 語句,一條新的記錄會添加到數(shù)據(jù)庫表中。
下面是 "insert.php" 頁面的代碼:
?php
$con?=?mysql_connect("localhost","peter","abc123");
if?(!$con)
{
die('Could?not?connect:?'?.?mysql_error());
}
mysql_select_db("my_db",?$con);
$sql="INSERT?INTO?Persons?(FirstName,?LastName,?Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if?(!mysql_query($sql,$con))
{
die('Error:?'?.?mysql_error());
}
echo?"1?record?added";
mysql_close($con)
?
網頁標題:php連數(shù)據(jù)庫插數(shù)據(jù),php和數(shù)據(jù)庫連接
當前鏈接:http://muchs.cn/article4/phjdie.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供營銷型網站建設、企業(yè)建站、網站維護、動態(tài)網站、網站導航、網站策劃
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)