十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
本文實(shí)例講述了PHP實(shí)現(xiàn)的pdo連接數(shù)據(jù)庫(kù)并插入數(shù)據(jù)功能。分享給大家供大家參考,具體如下:
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供監(jiān)利網(wǎng)站建設(shè)、監(jiān)利做網(wǎng)站、監(jiān)利網(wǎng)站設(shè)計(jì)、監(jiān)利網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、監(jiān)利企業(yè)網(wǎng)站模板建站服務(wù),十多年監(jiān)利做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
創(chuàng)建配置文件
pdo_config.php
?php
$db_Type
=
"mysql";//數(shù)據(jù)庫(kù)類型
$host
=
"localhost";//主機(jī)名
$dbName
=
"test";//數(shù)據(jù)庫(kù)名
$userName
=
"root";//用戶名
$password
=
"root";//密碼
$dsn
=
"{$db_Type}:host={$host};dbname={$dbName}";
?
pdo插入數(shù)據(jù)庫(kù)
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)建一個(gè)連接對(duì)象
$pdo-exec('set
names
utf8');//設(shè)置編碼
$sql
=
"INSERT
student
(name,email)
VALUES
('李四','123@qq點(diǎn)抗 ')";
$pdo-exec($sql);
}catch
(PDOException
$e){
die('操作失敗'.$e-getMessage());
}
//關(guān)閉連接
$pdo
=
null;
?
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫(kù)技巧總結(jié)》、《php+mysqli數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:關(guān)于php連接mssql:pdo
odbc
sql
serverPHP5中使用PDO連接數(shù)據(jù)庫(kù)的方法PHP中PDO連接數(shù)據(jù)庫(kù)中各種DNS設(shè)置方法小結(jié)ThinkPHP框架基于PDO方式連接數(shù)據(jù)庫(kù)操作示例PHP使用ODBC連接數(shù)據(jù)庫(kù)的方法tp5(thinkPHP5)框架連接數(shù)據(jù)庫(kù)的方法示例PHP7使用ODBC連接SQL
Server2008
R2數(shù)據(jù)庫(kù)示例【基于thinkPHP5.1框架】tp5(thinkPHP5)操作mongoDB數(shù)據(jù)庫(kù)的方法thinkPHP5實(shí)現(xiàn)數(shù)據(jù)庫(kù)添加內(nèi)容的方法tp5(thinkPHP5)框架數(shù)據(jù)庫(kù)Db增刪改查常見操作總結(jié)PHP利用pdo_odbc實(shí)現(xiàn)連接數(shù)據(jù)庫(kù)示例【基于ThinkPHP5.1搭建的項(xiàng)目】
1、 我們需要接收一個(gè)外部的訂單,而這個(gè)訂單號(hào)是不允許重復(fù)的
2、 數(shù)據(jù)庫(kù)對(duì)外部訂單號(hào)沒有做唯一性約束
3、 外部經(jīng)常插入相同的訂單,對(duì)于已經(jīng)存在的訂單則拒絕處理
對(duì)于這個(gè)需求,很簡(jiǎn)單我們會(huì)用下面的代碼進(jìn)行處理(思路:先查找數(shù)據(jù)庫(kù),如果數(shù)據(jù)庫(kù)存在則直接退出,否則插入)
package com.yhj.test;
import com.yhj.dao.OrderDao;
import com.yhj.pojo.Order;
/**
* @Description:并發(fā)測(cè)試用例
* @Author YHJ create at 2011-7-7 上午08:41:44
* @FileName com.yhj.test.TestCase.java
*/
public class TestCase {
/**
* data access object class for deal order
*/
private OrderDao orderDao;
/**
* @Description:插入測(cè)試
* @param object 要插入的object實(shí)例
* @author YHJ create at 2011-7-7 上午08:43:15
* @throws Exception
*/
public void doTestForInsert(Order order) throws Exception {
Order orderInDB = orderDao.findByName(order.getOrderNo());
if(null != orderInDB)
throw new Exception("the order has been exist!");
orderDao.save(order);
}
}
建立好數(shù)據(jù)庫(kù)后,
?php
$con = mysql_connect("數(shù)據(jù)庫(kù)地址","用戶名","密碼");
if (!$con) { die('鏈接錯(cuò)誤: ' . mysql_error()); }
mysql_select_db("要使用的Table", $con);
mysql_query("INSERT INTO 表名 (字段1, 字段2, 字段N) VALUES ('字段1數(shù)據(jù)', '字段2數(shù)據(jù)', '字段3數(shù)據(jù)')");
mysql_close($con);
?
一般情況下很少單獨(dú)的這樣使用,都是做成類。
你可以下載一個(gè)speedphp框架來參考一下。閱讀這個(gè)框架的源碼,對(duì)初學(xué)者來說相當(dāng)?shù)挠袔椭?/p>
手打不謝