将会话变量传递给另一个页面

I am trying to pass session variable to the next page, index.php but i have gotten an error undefined index on foreach($_SESSION['result'] as $row). May i know what's wrong?

<?php
session_start();
$search = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
$search = $db->real_escape_string($search);

if (strlen($search) >= 1 && $search !== ' ') {
    $query = 'SELECT * FROM tablename WHERE name LIKE "%'.$search.'%"';
    $result = $db->query($query) or trigger_error($db->error."[$query]");


    while($results = $result->fetch_array()) {
        $resultArray[] = $results;
    }   

    if (isset($resultArray)) {
        foreach ($resultArray as $result) {
            $show_name = preg_replace("/".$search."/i", "<b class='highlight'>".$search."</b>", $result['name']);
            $show_url = 'index.php';
            $out = str_replace('name', $show_name, $html);
            $out = str_replace('url', $show_url, $out); 
            $_SESSION['result']= $result['name'];
            echo($out);
        }
    }
}
?>

index.php

session_start();
<table>
<?php foreach($_SESSION['result'] as $row){ ?>
            <tr>
              <td>
                <?php echo $row['name'];?>
              </td>
              <td>
                <?php echo $row['description'];?>
              </td>
            </tr>
<?php } ?>
</table>

You never start the session in the first file. Start the file with

<?php    
session_start();

Because you're missing that, when you assign a value to $_SESSION['result'] after your queries, nothing is saved in session.

There is another error. You mean for $_SESSION['result'] to be an array, but you're saving it as a string. Change:

$_SESSION['result']= $result['name'];

To

$_SESSION['result'][]= $result['name'];

if $_SESSION['result'] is an array then try like below

$_SESSION['result'][]= $result['name'];

as per your code session result variable is not an array