Mysqli_fetch_assoc不存储为会话变量

时间:2014-04-11 20:39:05

标签: php mysql session-variables

我正在尝试将mySQL查询中输出的表存储到会话变量中,并在另一个页面中使用。这是我到目前为止所做的,但我没有得到输出:

显示搜索结果的第一页:

<?php
session_start();
$personid = ($_POST['personid']) ? $_POST['personid'] : $_GET['personid'];


if (empty($personid)) {
echo 'Please enter some search parameters';
} else {

$sql = "SELECT * FROM persons WHERE 1=1";

if ($personid)
$sql .= " AND personid='" . mysqli_real_escape_string($mysqli,$personid) . "'";

$total_records = mysqli_num_rows(mysqli_query($mysqli,$sql));

$sql .= " ORDER BY surname";

$loop = mysqli_query($mysqli,$sql)
or die ('cannot run the query because: ' . mysqli_error($mysqli));

  echo "<table border='1' cellpadding='10'>";
    echo "<tr> <th>Name</th> <th>Address</th> <th>City</th> <th>Province</th>    <th>Postal Code</th> <th>Phone Number</th> <th>Email</th> <th></th></tr>";

while ($record = mysqli_fetch_assoc($loop)) {
echo "<tr>";
          /*  echo '<td>' . $record['firstname'] .   $record['surname'] .'</td>';*/
             echo "<td><a href=\"person.php?$record[personid]\">$record[firstname]   $record[surname]</a></td>";
            echo '<td>' . $record['address'] . '</td>';
            echo '<td>' . $record['city'] . '</td>';
            echo '<td>' . $record['province'] . '</td>';
            echo '<td>' . $record['postalcode'] . '</td>';
            echo '<td>' . $record['phone'] . '</td>';
            echo '<td>' . $record['email'] . '</td>';

            echo ("<td><a href=\"records.php?$record[personid]\">Edit</a></td>");

            echo "</tr>"; 


 }
 echo "</table>";


$_SESSION['animals']=$loop;

echo "<center>" . number_format($total_records) . " search results found</center>";



}

?>
<a href="passthru.php" target="_blank">Click here to see if sessions work</a>

然后转到这个脚本:

<?php
session_start();

echo "The results";



 while ($record = mysqli_fetch_assoc($_SESSION['animals'])) {
echo "<tr>";
             echo "<td><a href=\"person.php?$record[personid]\">$record[firstname]   $record[surname]</a></td>";
            echo '<td>' . $record['address'] . '</td>';
            echo '<td>' . $record['city'] . '</td>';
            echo '<td>' . $record['province'] . '</td>';
            echo '<td>' . $record['postalcode'] . '</td>';
            echo '<td>' . $record['phone'] . '</td>';
            echo '<td>' . $record['email'] . '</td>';

            echo ("<td><a href=\"records.php?$record[personid]\">Edit</a></td>");

            echo "</tr>"; 


}

问题可能是什么?提前谢谢。

3 个答案:

答案 0 :(得分:3)

会话用于存储原始数据类型。

将结果集存储到会话中是一个糟糕的想法,并且很可能对服务器资源不利。

如果您确实需要应用程序中的其他位置的数据,只需再次查询您的数据库,这不是太贵。

答案 1 :(得分:0)

我可以看到您尝试将mysqli资源对象存储到会话中的问题。

$loop = mysqli_query($mysqli,$sql)

如果要将最终的html存储到会话中,则创建一个包含最终html字符串的变量。

$html = "<table>";
while ($record = mysqli_fetch_assoc($loop)) {
  $html .= $record['something']; 
      // Likewise add other variables to html string. 

  }
$html . = "</table>";
$_SESSION['animals'] = $html   

但是如果你想要包含所有数据库结果的数组,那么你可以这样做

$rows = array();
while ($record = mysqli_fetch_assoc($loop)) {    
     $rows[] = $record; 
}
$_SESSION['animals'] = $rows;

现在您在其他页面中使用此会话值并迭代并创建html。

答案 2 :(得分:-1)

正如其他具有会话变量的网站一样,使用isset将使脚本顺利运行

if (!isset($_SESSION['basket'])){
$_SESSION['basket'] = array();

}

帕斯卡尔 multiskillz