php同步數據
❶ php多條數據如何一起提交
參考如下
多個提交和一個提交的道理是相同的,只是一些細節上要注意。
提交一個,表單是:
<form>
<input type=text name=name>
<input type=text name=sex>
<input type=text name=age>
<input type=text name=address>
</form>
PHP存資料庫的語句是:
$sql="insert into tab(...) values ($_POST[...])";//省略欄位和值
那麼多個提交的方法一,表單是:
<form>
<input type=text name=name1><input type=text name=sex1><input type=text name=age1><input type=text name=address1>
<input type=text name=name2><input type=text name=sex2><input type=text name=age2><input type=text name=address2>
</form>
PHP存資料庫語句是:
$sql="insert into tab(...) values ($_POST[...1])";//省略欄位和值
mysql_query($sql);
$sql="insert into tab(...) values ($_POST[...2])";//省略欄位和值
mysql_query($sql);
上面方法一寫的例子是兩條,多條的方法相同,技巧就是輸出表單使用JS的循環,存檔的PHP代碼也可以循環,並且能夠判斷為空的就不提交,比如表單20條,只填了5條,就只存5條到資料庫。
方法二是使用數組,表單:
<form>
<input type=text name=name><input type=text name=sex><input type=text name=age><input type=text name=address>
<input type=text name=name><input type=text name=sex><input type=text name=age><input type=text name=address>
<input type=text name=name><input type=text name=sex><input type=text name=age><input type=text name=address>
</form>
PHP代碼是:
for ($i=0;$i<count($_POST["name"]);$i++)
if ($_POST["name"][$i]!='')
{
$sql="insert into tab(...) values ($_POST[...][$i])";//省略欄位和值
mysql_query($sql);
}
這樣表單可以寫任意多行,PHP裡面是數組,能夠自動獲取有多少數據。
❷ php怎麼連接資料庫
在PHP中連接資料庫,一般使用PDO或者MySQLi擴展庫來實現。
使用PDO連接資料庫時,你需要先創建一個新的PDO實例,提供資料庫的連接信息,如DSN、用戶名和密碼。例如:$pdo = new PDO;。之後,你就可以使用這個$pdo對象來執行SQL查詢和其他資料庫操作了。
而使用MySQLi連接資料庫,你需要創建一個mysqli對象,並傳入資料庫的連接信息。例如:$mysqli = new mysqli;。連接成功後,你也可以通過這個$mysqli對象來進行資料庫操作。
這兩種方式都能有效地連接和操作資料庫,選擇哪一種主要取決於你的具體需求和編程習慣。不過,一般來說,PDO的跨資料庫兼容性更好,而MySQLi則提供了更多的MySQL特定功能。
請注意,為了安全起見,不要在代碼中硬編碼資料庫的用戶名和密碼。最好是從配置文件或環境變數中讀取這些信息。同時,確保你的代碼能夠妥善處理資料庫連接失敗的情況。
❸ php如何實現兩台伺服器資料庫同步問題 - 技術問答
基於資料庫 Log 日誌分析可以實現,網上搜一下 CDC 數據同步。。
不過你也可以嘗試下 cloud.tapdata.net , 一個在線的數據同步工具,支持一次性全量同步,也支持實時的增量同步。
❹ 請問php怎樣抓取其它網站的動態數據,顯示在自己的網頁內並同步更新。
剛吃完午飯吧,來幫你實現一下吧。記得加分哦。
$url = "http://www.boc.cn/sourcedb/whpj/";
$queryServer = curl_init();
curl_setopt($queryServer, CURLOPT_URL, $url);
curl_setopt($queryServer, CURLOPT_HEADER, 0);
curl_setopt($queryServer, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($queryServer, CURLOPT_RETURNTRANSFER, true);
curl_setopt($queryServer, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($queryServer, CURLOPT_TIMEOUT, 30);
$html = curl_exec($queryServer);
$html = iconv('UTF-8','GBK//IGNORE',$html); //如果你需要是的數據是utf-8編碼的,這一行可以注銷,如果需要gbk編碼的,請保留.如果出現亂碼,就是一行的問題,你自己調著試吧
//echo $holder;exit; 此處可以輸出來測試.
$html = str_replace(array("\n","\r","\t"),"",$html);
$preg = '/<table\s+width=\"800\"[^>]+>(.*?)<\/table>/';
preg_match_all($preg,$html,$out);
//匹配每行
preg_match_all('/<tr[^>]+>(.*?)<\/tr>/',$out[1][0],$tr);
//匹配每個td
$result = array();
$match = '/<td.+>([^<]+)<\/td>/U';
foreach( $tr[0] as $key => $value ){
preg_match_all($match,$value,$arr);
$result[] = $arr[1];
}
//輸出測試,$result就是你要的數據,至於你要怎麼輸出顯示格式,那就隨心調就好了。
foreach( $result as $key => $value ){
echo implode("\t",$value);
echo "<br>";
}
exit;