开发者

session_start() in PHP

开发者 https://www.devze.com 2023-04-12 21:16 出处:网络
<?php session_start();?> HTML <?php // code for mysql... $number = mysql_num_rows($result); // it gets the correct value. Tested with echo.
<?php session_start();?>
    HTML
<?php 
    // code for mysql...
    $number = mysql_num_rows($result);
    // it gets the correct value. Tested with echo.
?>
    HTML
<?php
$i=0;
while ($number > $i) 
{ 
    $id = mysql_result($result,$i,"Id");
    $address = mysql_result($result,$i,"address");
    $title = mysql_result($result,$i,"title");
    $_SESSION[i] = $id;
}
?>
<option value="<?php echo"$开发者_如何学运维_SESSION[i]"; ?>">
<?php echo"$address $title"; ?>
</option>
<?php
$i++;
?>

This code add just only one element to option. If i comment session_start() then all the value are inserted in the select. But if i don't use session_start then I cannot verify the user logged.

What's happen to my code?


I assume you mean $i inside the $_SESSION[] array, rather than the constant i:

$_SESSION[i] = $id;
?><OPTION value="<?php echo"$_SESSION[i]"; ?>">

// Should be
$_SESSION[$i] = $id;
?><OPTION value="<?php echo"$_SESSION[$i]"; ?>">

PHP will convert the unknown constant i into a string "i", which it assumes you intended. Really I think you meant to use the variable $i.


Instead of calling mysql_result three times for each row, might I suggest using mysql_fetch_array / mysql_fetch_assoc / mysql_fetch_row to grab the entire row in one call? Then you can access each element like:

$row = mysql_fetch_array( $result);
$id = $row['Id'];
$address = $row['address'];

The PHP manual has an example on how to loop on the number of rows returned from the database.

http://us3.php.net/manual/en/function.mysql-fetch-array.php

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号