PHP連接Access數(shù)據(jù)庫
創(chuàng)新互聯(lián)建站專注于定興網(wǎng)站建設服務及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供定興營銷型網(wǎng)站建設,定興網(wǎng)站制作、定興網(wǎng)頁設計、定興網(wǎng)站官網(wǎng)定制、小程序制作服務,打造定興網(wǎng)絡公司原創(chuàng)品牌,更為您提供定興網(wǎng)站排名全網(wǎng)營銷落地服務。
?php
/*
創(chuàng)建ADO連接
*/
$conn = new COM("ADODB.Connection");
$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("1.mdb");
$conn-Open($connstr);
/*
創(chuàng)建記錄集查詢
*/
$rs = new COM("ADODB.RecordSet");
$rs-Open("select * from table1",$conn,1,3);
/*
循環(huán)讀取數(shù)據(jù)
*/
while(!$rs-eof){
echo $rs-Fields["id"]-Value;
echo "br/";
$rs-Movenext();
}
$rs-close();
?
最近有一個需求,前端向后臺提交json,后臺解析并且將提交的值插入數(shù)據(jù)庫中,
難點
1、php解析json(這個不算難點了,網(wǎng)上實例一抓一大把)
2、解析json后,php怎樣拿到該拿的值
?php
require
('connect.php');
/*
本例用到的數(shù)據(jù):
post_array={"order_id":"0022015112305010013","buyer_id":"2","seller_id":"1","all_price":"100.00","json_list":[{"product_id":"3","product_number":"3"},{"product_id":"8","product_number":"2"},{"product_id":"10","product_number":"4"}]}
*/
$post_array=$_POST['post_array'];
//--解析Json,獲取對應的變量值
$obj=json_decode($post_array,TRUE);
$order_id
=
$obj['order_id'];
$buyer_id
=
$obj['buyer_id'];
$seller_id
=
$obj['seller_id'];
$all_price
=
$obj['all_price'];
$i=0;//循環(huán)變量
//--得到Json_list數(shù)組長度
$num=count($obj["json_list"]);
//--遍歷數(shù)組,將對應信息添加入數(shù)據(jù)庫
for
($i;$i$num;$i++)
{
$list_product_id[]=$obj["json_list"][$i]["product_id"];
$list_product_number[]=$obj["json_list"][$i]["product_number"];
$insert_order_product_sql="INSERT
INTO
tbl_order_product
(order_id,product_id,product_number)
VALUES
(?,?,?)";
$result
=
$sqlconn
-
prepare($insert_order_product_sql);
$result
-
bind_param("sss",
$order_id,$list_product_id[$i],$list_product_number[$i]);
$result-execute();
}
//--添加訂單信息
$insert_order_sql="INSERT
INTO
tbl_order
(order_id,buyer_id,seller_id,all_price)
VALUES
(?,?,?,?)";
$result=$sqlconn-prepare($insert_order_sql);
$result-bind_param("ssss",$order_id,$buyer_id,$seller_id,$all_price);
$result-execute();
$result
-
close();
$sqlconn
-
close();
?
投稿者信息
昵稱:
Hola
Email:
jamcistos@outlook.com
本文實例講述了PHP實現(xiàn)的pdo連接數(shù)據(jù)庫并插入數(shù)據(jù)功能。分享給大家供大家參考,具體如下:
創(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搭建的項目】
把來自表單的數(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)
?