在PHP中将MySQL转换为MSSQL:会话中的未识别索引

时间:2016-05-02 05:08:10

标签: php sql-server

我将我的代码从PHP,MySQL转换为sqlsrv,并且出于某种原因我的购物车'被称为不明的索引。知道为什么吗?

<?php
if ($_SERVER['REQUEST_URI'] != '/index.php?page=cart') {         
    $sql="SELECT * FROM table WHERE id IN ("; 

    foreach($_SESSION['cart'] as $id => $value) { 
        $sql.=$id.","; 
    } 

    $sql=substr($sql, 0, -1).") ORDER BY name ASC"; 
    @$query= sqlsrv_query($conn, $sql); 
    $numrows = sqlsrv_num_rows($query);
    if($numrows != 0) {
        ?><h1>Current Order</h1><?php while($row= sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){
            ?> 
            <p> <?php echo $row['name'] ?> X <b><?php echo $_SESSION['cart'][$row['id_product']]['quantity']  ?></b></p> 
            <?php      
        } 
        ?>

1 个答案:

答案 0 :(得分:1)

session_start()代码

之后添加<?php
session_start();    

if ($_SERVER['REQUEST_URI'] != '/index.php?page=cart') 
{             
     $sql="SELECT * FROM table WHERE id IN ("; 
     foreach($_SESSION['cart'] as $id => $value) 
     { 
            $sql.=$id.","; 
     } 

它会对你有用,也可以isset检查$_SESSION['cart']然后再使用它。