dougua3705 2012-06-06 03:36
浏览 57
已采纳

PHP:为什么不在类中识别变量PHPSESSID?

As the title says,, I want to know because it does not recognize the $ sessionID variable within the class DB_Functions.

When I check the table, all fields except the variable sessionID stored.

<?php

session_start();
$sessionID = $_COOKIE['PHPSESSID'];

class DB_Functions {
....
....
....

public function insertItemToCart($dataMovieToInsertCart) {
        $precio = $dataMovieToInsertCart['precioMovie'];
        $id =  $dataMovieToInsertCart['claveMovie'];

        $query = "INSERT INTO carrito (session_carrito,id_pelicula,precio_pelicula) VALUES('$sessionID','$id','$precio')";
        $result = mysql_query($query) or die (mysql_error());

        if ($result) {
            return true;
        } else {
            return false;
        }
    }

}
?>
  • 写回答

1条回答 默认 最新

  • dongyu2764 2012-06-06 03:43
    关注

    two way fix:

    1.) use global keyword inside the function to catch up with global variables which are not available inside function scope

    2.) if its only the case of session_id, use session_id() function inside function.

    eg for 1:

    global $sessionID;
    $id =  $dataMovieToInsertCart['claveMovie'];
    $query = "INSERT INTO carrito (session_carrito,id_pelicula,precio_pelicula) VALUES('$sessionID','$id','$precio')";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?