避免出现"Notice: Use of undefined constant......"警告

来源:互联网 发布:淘宝旺铺首页全屏装修 编辑:程序博客网 时间:2024/05/22 13:28

刚开始学习php,写了一段测试程序,如下:

<?php
$username = $_POST[username];//从html表单获取数据;
$password = $_POST[password];

$link = mysql_connect('localhost','root','123') or die('could not connect:'.mysql_error()) ;
$select = mysql_select_db('blksun',$link) or die('could not select database') ;

$query = 'select * from userlist where userName="'.$username.'"';
$result = mysql_query($query) or die('query failed:'.mysql_error()) ;

echo "<table border=1>" ;
echo "<tr><td>id</td><td>username</td><td>userpwd</td></tr>" ;
while ($array = mysql_fetch_assoc($result)) 
{
 printf("<tr><td>%d</td><td>%s</td><td>%s</td></tr>",$array[Id],$array[userName],$array[userPwd]);
}
echo "</table>" ;

// 释放结果集
mysql_free_result($result);
// 关闭连接
mysql_close($link);
?>

以上程序运行时就会出现诸如:“Notice: Use of undefined constant username - assumed 'username' inD:/wwwroot/userreg.php on line 2
的提示,但是结果也能显示出来。要想不显示“notice”,就得将上面程序中红色的变量,加上引号。

另外:$array[Id],$array[userName],$array[userPwd] 必须大小写区分。

转自:http://hi.baidu.com/liheng_2009/item/6eca8c7000dd423f6e29f694

0 0
原创粉丝点击