2010-02-04 159 views
1

我想通過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中包含太多的代碼?似乎必須有更好的方式...

+0

'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

回答

2

你爲什麼不這樣做(如其他已經提到,$IDNUM不在功能範圍內):

function refreshCurrentProduct() { 
    $dbh=mysql_connect ("DATABASE","USER", "PASS") or die('I cannot connect to the database because:'. mysql_error()); 
    mysql_select_db("TABLE"); 

    // If $_GET['idNum'] is not a number use 0 
    $rowNumber = is_numeric($_GET['idNum']) ? $_GET['idNum'] : 0; 

    $query = "SELECT ID, STOCK, MYNAME, DESCRIPTION, PRICE, SHIPPING FROM `PRODUCTS`"; 
    $result = mysql_query($query); 

    if(mysql_data_seek($result, $rowNumber)) { 
     // The result set has indeed at least $rowNumber rows 

     $row = mysql_fetch_assoc($result); 

     echo $row['ID']; 
     echo $row['STOCK']; 
     // ... etc .... 
    } 
    else { 
     echo "No such row!"; 
    } 
} 

不需要打六次數據庫!當然你需要添加錯誤處理。

Btw。是參數idNum與數據庫中記錄的ID相同嗎?如果是這樣,您甚至可以進一步簡化:

function refreshCurrentProduct() { 
    $dbh=mysql_connect ("DATABASE","USER", "PASS") or die('I cannot connect to the database because:'. mysql_error()); 
    mysql_select_db("TABLE"); 

    // If $_GET['idNum'] is not a number use 0 
    $id = is_numeric($_GET['idNum']) ? $_GET['idNum'] : 0; 

    $query = "SELECT ID, STOCK, MYNAME, DESCRIPTION, PRICE, SHIPPING FROM `PRODUCTS` WHERE ID = $id"; 
    $result = mysql_query($query); 

    if (mysql_num_rows($result) == 0) { 
     echo "No rows found, nothing to print"; 
     return; 
    } 

    $row = mysql_fetch_assoc($result); 

    echo $row['ID']; 
    echo $row['STOCK']; 
    // ... etc .... 
} 
+0

+2用於SQL改進 – outis 2010-02-04 20:07:59

+0

謝謝Felix! – Jared 2010-02-08 13:53:13

2

看看call_user_func

$functionName = $_GET[ 'func' ]; 

if(in_array($functionName, $allowedFunctions) && function_exists($functionName)) 
{ 
    call_user_func($functionName); 
} 

而且,如果我在讀你的代碼的權利,你可以得到所有的信息在一個單一的查詢:

$query = "SELECT `ID`,`STOCK`,`MYNAME`,`DESCRIPTION`,`PRICE`,`SHIPPING` FROM `PRODUCTS`"; 
$result = mysql_query($query) or die('Query failed:'.mysql_error()); 
while ($row = mysql_fetch_assoc($result)) { 
    $ID=$row['ID']; 
    //etc. 
} 
+0

不完全,OP想要獲得某一行,而不是所有的行。 – 2010-02-04 19:50:40

+0

Btw'$ functionName()'不是OP的問題。這按預期工作。 – 2010-02-04 20:06:18

1

您的$IDNUM變量超出了您的函數範圍。你或者需要將它作爲變量傳遞給你的函數,或者你應該可以通過在函數內部設置它來設置它。

function refreshCurrentProduct() { 
    $IDNUM = $_GET[ 'idNum' ]; 
    ... 
} 
+0

謝謝你的幫助! – Jared 2010-02-08 13:53:58