我試圖能夠將代理列表粘貼到文本文件中,並且能夠將行狀態更新爲1,如果粘貼的代理位於數據庫中。我得到了它的工作即時通訊使用爆炸分離列表,並從那裏使用查詢函數foreach它。問題是它不更新。它只適用於最後一個代理。PHP - Foreach SQL查詢
查詢功能:
function Connect($host = false,$username = false,$password = false,$dbname = false) {
//Try execute the PHP with no errors;
try {
//Create a PDO Session;
$con = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
//Session Attributes;
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
//Catch all PDOException errors;
catch (PDOException $e) {
//Make the PDO session false;
$con = false;
}
//If no errors happened Make the PDO session true;
return $con;
}
//Create a new function named query;
function query($sql = false,$dbname = false,$bind = false,$obj = false) {
//Prepare The SQL Query;
$query = Connect('localhost','xx','xx',$dbname)->prepare($sql);
$res = true;
//Execute Binded Query;
try { $query->execute($bind); $res = true; }
catch (PDOException $e) {
$res = false;
}
//If no errors happened Make $row true;
return $res;
}
和PHP:
<?php
//Define if Error Logging will be turned on (true) or off (false);
$errorlogging = true;
$obstart = false;
$adminonly = false;
//Set the default Timezone to UK/Ireland;
date_default_timezone_set('Europe/London');
//Globalized functionality needed on all pages;
require('common/requisites.php');
//Include The Database Connection;
require('common/db.php');
if(!empty($_POST['proxies'])) {
$proxyinput = $_POST['proxies'];
$proxies = explode("\n",$proxyinput);
foreach($proxies as $proxy) {
query('UPDATE proxies SET status = 1 WHERE :proxy = proxy','unfed_tools',array(':proxy'=>$proxy));
echo "Updated: ".$proxy."\n\r";
}
}
?>
<form action="" method="post">
<textarea name="proxies" style="height: 300px; width: 300px;"></textarea>
<button type="submit">Click</button>
</form>
',代理=:proxy' –
@u_mulder不知道我怎麼錯過了,但它仍然只是工作的最後一個代理。 –
循環是否執行多次/你會得到多個「更新:」消息? – trincot