我想通過PHP中的URL中的多個變量來獲取一些信息,但我不認爲它的工作。如何在PHP中通過URL傳遞多個變量?
$allowedFunctions = array(
'returnAllProducts',
'refreshCurrentProduct'
);
$IDNUM = $_GET[ 'idNum' ];
$functionName = $_GET[ 'func' ];
if(in_array($functionName, $allowedFunctions) && function_exists($functionName))
{
$functionName();
}
然後我有refreshCurrentProduct功能:
function refreshCurrentProduct() {
$dbh=mysql_connect ("DATABASE","USER", "PASS") or die('I cannot connect to the database because:'. mysql_error());
mysql_select_db("TABLE");
$query = "SELECT `ID` FROM `PRODUCTS`";
$result = mysql_query($query) or die('Query failed:'.mysql_error());
$DB_STOCK = mysql_query("SELECT `STOCK` FROM `PRODUCTS`") or die('Query failed:'.mysql_error());
$DB_SHORT = mysql_query("SELECT `MYNAME` FROM `PRODUCTS`") or die('Query failed:'.mysql_error());
$DB_LONG = mysql_query("SELECT `DESCRIPTION` FROM `PRODUCTS`") or die('Query failed:'.mysql_error());
$DB_PRICE = mysql_query("SELECT `PRICE` FROM `PRODUCTS`") or die('Query failed:'.mysql_error());
$DB_SHIP = mysql_query("SELECT `SHIPPING` FROM `PRODUCTS`") or die('Query failed:'.mysql_error());
$ID = mysql_result($result,$IDNUM,"ID");
$STOCK = mysql_result($DB_STOCK,$IDNUM,"STOCK");
$SHORT = mysql_result($DB_SHORT,$IDNUM,"MYNAME");
$LONG = mysql_result($DB_LONG,$IDNUM,"DESCRIPTION");
$PRICE = mysql_result($DB_PRICE,$IDNUM,"PRICE");
$SHIP = mysql_result($DB_SHIP,$IDNUM,"SHIPPING");
echo '
//echo $STOCK, $SHORT, etc....
';
}
我使用的URL是products.php?func=refreshCurrentProduct&idNum=4
從理論上講,應該從排在它4顯示,但是,只有它顯示第一行的信息。如果我在函數中做了$IDNUM=5
,它會顯示第5行,所以我傳遞信息的方式有些問題。
另外,如何創建(例如)$ STOCK而不必在$ DB_STOCK中包含太多的代碼?似乎必須有更好的方式...
'mysql_error'公開錯誤消息的詳細信息(http://msdn.microsoft.com/en-us/library/ms995351.aspx#securityerrormessages_topic2)。此外,舊的MySQL驅動程序相當過時。看看PDO(http://php.net/PDO)並準備報表。 – outis 2010-02-04 20:07:30