dtvnnhh8992 2010-02-04 19:34
浏览 333
已采纳

如何通过php中的URL传递多个变量?

I am trying to pass multiple variables in a URL in PHP to GET some info, but I don't think it's working.

$allowedFunctions = array(
   'returnAllProducts',
   'refreshCurrentProduct'

);


$IDNUM = $_GET[ 'idNum' ];


$functionName = $_GET[ 'func' ];

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

Then I have the refreshCurrentProduct function:

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....

    ';
 }

The URL I am using is products.php?func=refreshCurrentProduct&idNum=4

In theory, that should display from the row with 4 in it, however, it only displays the info from the first row. If I do a $IDNUM=5 within the function, it will display the 5th row, so something is wrong with how I pass the information.

Also, how do I create (for instance) $STOCK without having to have so much code in $DB_STOCK? Seems like there has to be a better way...

  • 写回答

3条回答 默认 最新

  • duanbi5906 2010-02-04 19:42
    关注

    Why don't you do (as others already mentioned , $IDNUM is not in the scope of the function):

    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!";
        }
    }
    

    No need to hit the database six times! Of course you need to add error handling.

    Btw. is the parameter idNum the same as the ID of the record in the database? If so, you can even further simplify:

    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 ....
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输