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')";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏