[英]session_start() in PHP
<?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.
如果我评论session_start(),则所有值都插入到select中。 But if i don't use session_start then I cannot verify the user logged.
但如果我不使用session_start,那么我无法验证用户登录。
What's happen to my code? 我的代码怎么了?
I assume you mean $i
inside the $_SESSION[]
array, rather than the constant i
: 我假设你在
$_SESSION[]
数组中的意思是$i
,而不是常量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. PHP会将未知常量
i
转换为字符串"i"
,它假设您有意。 Really I think you meant to use the variable $i
. 真的,我认为你打算使用变量
$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? 我建议使用mysql_fetch_array / mysql_fetch_assoc / mysql_fetch_row在一次调用中抓取整行,而不是每行调用三次mysql_result? 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. PHP手册有一个关于如何循环从数据库返回的行数的示例。
http://us3.php.net/manual/en/function.mysql-fetch-array.php http://us3.php.net/manual/en/function.mysql-fetch-array.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.