action 到 doing.php 就要用post或者get之后連接數(shù)據(jù)庫,插入數(shù)據(jù)并顯示數(shù)據(jù),下面是個(gè)小例子也有注釋
創(chuàng)新互聯(lián)一直在為企業(yè)提供服務(wù),多年的磨煉,使我們?cè)趧?chuàng)意設(shè)計(jì),營銷型網(wǎng)站建設(shè)到技術(shù)研發(fā)擁有了開發(fā)經(jīng)驗(yàn)。我們擅長(zhǎng)傾聽企業(yè)需求,挖掘用戶對(duì)產(chǎn)品需求服務(wù)價(jià)值,為企業(yè)制作有用的創(chuàng)意設(shè)計(jì)體驗(yàn)。核心團(tuán)隊(duì)擁有超過10多年以上行業(yè)經(jīng)驗(yàn),涵蓋創(chuàng)意,策化,開發(fā)等專業(yè)領(lǐng)域,公司涉及領(lǐng)域有基礎(chǔ)互聯(lián)網(wǎng)服務(wù)棕樹數(shù)據(jù)中心、成都APP應(yīng)用開發(fā)、手機(jī)移動(dòng)建站、網(wǎng)頁設(shè)計(jì)、網(wǎng)絡(luò)整合營銷。
?php
$data['class'] = $_POST['class'] + 0;
$data['name'] = trim($_POST['name']);
$data['math'] = $_POST['math'] + 0;
$data['chemistry'] = $_POST['chemistry'] + 0;//以下的自己寫
/*連接數(shù)據(jù)庫 插入數(shù)據(jù)*/
$conn = mysql_connect('url','username','password');
$sql = 'use db_name';
mysql_query($sql,$conn);
//這個(gè)其實(shí)可以封裝成一個(gè)更好的丟向方法就不寫了
foreach($data as $k=$v){
$sql = 'insert into db('.$k.')value("'.$v.'")';
mysql_query($sql,$conn);
}
/*取出數(shù)據(jù)并打印*/
$sql = 'select class,name,math,chemistry from table_name order by class';
$res = mysql_query($sql,$conn);
echo 'table id="score"trtd班級(jí)/tdtd名字/tdtd數(shù)學(xué)/tdtd化學(xué)/td/tr';
while($row = mysql_fetch_assoc($res)){//取關(guān)系數(shù)組,打印
echo 'trtd'.$row['class'].'/tdtd'.$row['name'].'/tdtd'.$row['math'].'/tdtd'.$row['chemistry'].'/td/tr';
}
echo '/table';?
PHP使用面向?qū)ο蟮木幊谭绞絹砭帉憯?shù)據(jù)庫操作類
步驟1:創(chuàng)建一個(gè)PHP的頁面“config.php”定義數(shù)據(jù)庫相關(guān)的參數(shù)
?php // config.php
define(?DB_USER?, "username");
define(?DB_PASSWORD?, "password");
define(?DB_DATABASE?, "database name");
define(?DB_SERVER?, "ip address of database server");
?
第2步:創(chuàng)建一個(gè)PHP的類,用于連接數(shù)據(jù)庫,命名為“db_connect.php”
?php // db_connnect.php
class DB_Connect {
private $con;
// constructor
function __construct() {
// connecting to database
$this-con = $this-connect();
}
//Function to connect with database
private function connect() {
// import database connection variables
require_once __DIR__.?/config.php?;
try {
$conn = new PDO(?mysql:host=?.DB_SERVER .?;
dbname=?.DB_DATABASE, DB_USER, DB_PASSWORD);
$conn-setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo ?ERROR: ? . $e-getMessage();
}
return $conn;
}
public function getDbConnection(){
return $this-con;
}
}
?
第3步:創(chuàng)建一個(gè)類,它包含了所有的功能,為您實(shí)現(xiàn)SQL查詢,命名為“db_functions.php”
調(diào)用里面的函數(shù)進(jìn)行SQL查詢、以促進(jìn)可重用性和可維護(hù)性
?php // db_functions.php
class DB_Functions {
private $con;
// constructor
function __construct() {
require_once __DIR__.?/db_connect.php?;
// connecting to database
$db = new DB_Connect();
$this-con = $db-getDbConnection();
}
public function selectUser($id) {
try {
$stmt = $this-con-prepare(?SQL語句?);
$params = array(?:id? = $id);
$stmt-execute($params);
return $stmt;
} catch(PDOException $e) {
echo ?ERROR: ? . $e-getMessage();
}
}
public function otherSQLfunction($parameter) {
// other sql code
}
}
第4步:最后,在你其他的PHP文件里面只需要簡(jiǎn)單地調(diào)用“db_functions.php”的方法
?php
require_once __DIR__.?/db_functions.php?;
$db = new DB_Functions();
$result = $db-selectUser($id);
// other code
?
設(shè)計(jì)思路么?
首先你需要設(shè)計(jì)數(shù)據(jù)庫,成績(jī)查詢需要設(shè)計(jì)哪些表,最簡(jiǎn)單的就是這幾三張表:學(xué)生表,課程表,成績(jī)表,然后設(shè)計(jì)每個(gè)表的字段和關(guān)聯(lián)關(guān)系
然后寫代碼,對(duì)數(shù)據(jù)庫進(jìn)行CURD,這種小系統(tǒng)完全不用考慮架構(gòu),數(shù)據(jù)量等,所以很簡(jiǎn)單的,數(shù)據(jù)庫+PHP服務(wù)端+web前端 最多1天就差不多能做好了
php和sql實(shí)現(xiàn)成績(jī)查詢主要是通過curl模擬用戶登錄實(shí)現(xiàn),不知道題主想要實(shí)現(xiàn)查詢什么成績(jī),這里有四六級(jí)的代碼供題主參考
//author?
//參數(shù)分別為準(zhǔn)考證號(hào),和姓名
function?siliuji_tab($zkzh,$xm){
$xm=substr(urlencode($xm),0,27);;
$url?=?"".$zkzh."xm=".$xm;
$refer="";
$cookie_file?=?tempnam('./temp','cookie');
$ip?=?'127.0.0.1';
$headers['CLIENT-IP']?=?$ip;
$headers['X-FORWARDED-FOR']?=?$ip;
$headerArr?=?array();
foreach?(?$headers?as?$n?=?$v?)
{
$headerArr[]?=?$n?.?':'?.?$v;
}
$ch?=?curl_init($url);
curl_setopt($ch,?CURLOPT_USERAGENT,?"Mozilla/5.0?(Windows?NT?6.2)?AppleWebKit/537.36?(KHTML,?like?Gecko)?Chrome/29.0.1547.76?Safari/537.36");
curl_setopt($ch,?CURLOPT_HTTPHEADER,?$headerArr);?
curl_setopt($ch,?CURLOPT_RETURNTRANSFER,?true)?;?//?獲取數(shù)據(jù)返回??
curl_setopt($ch,?CURLOPT_BINARYTRANSFER,?true)?;?//?在啟用?
curl_setopt($ch,?CURLOPT_COOKIEJAR,?$cookie_file);
curl_setopt($ch,?CURLOPT_COOKIEFILE,?$cookie_file);
curl_setopt($ch,?CURLOPT_HEADER,?1);
curl_setopt($ch,?CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,?CURLOPT_REFERER,?$refer);?
$result?=?curl_exec($ch);
}
?php
function?add($student,$id,$score){//添加學(xué)生
$student[$id]=$score;
}
function?print_score($student,$id){//輸入學(xué)號(hào),打印成績(jī)
echo?$student[$id];
}
function?print_id($student,$score){//輸入成績(jī),打印學(xué)號(hào)
foreach($student?as?$key=$val){
$val==$score??print($key."br?/");
}
}
function?sum_score($student,$min,$max){//統(tǒng)計(jì)介于min和max的分?jǐn)?shù)
foreach($student?as?$key=$val){
if($val=$min??$val=$max){
echo?$key.":".$val."br?/";
}
}
}
function?del($student,$id){//輸入學(xué)號(hào),刪除成績(jī)
unset($student[$id]);
}
//測(cè)試
$student=array();
add($student,"1","90");
add($student,"2","77");
add($student,"3","83");
add($student,"4","78");
add($student,"5","90");
print_score($student,"4");
print_id($student,"90");
sum_score($student,80,100);
?//請(qǐng)采納,如需詳細(xì),請(qǐng)說明
下載MYSQL 安裝 創(chuàng)建數(shù)據(jù)庫
下載PHP環(huán)境 ?WAMP并安裝,下載編輯器例如sublime text,下載熟悉的php框架例如CI 將CI包解壓至wamp的www文件夾下,使用sublime text 打開文件夾,配置CI框架內(nèi)的數(shù)據(jù)庫等信息,使用CI框架編寫程序;