<?php
$username="xxx";
$password="xxx";
$database="mobile_app";
mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
foreach (array('courseid','roomChosen') as $varname) {
$$varname = (isset($_POST[$varname])) ? $_POST[$varname] : '';
}
if (isset($_POST['prequestion'])) {
$roomquery = "
SELECT Room
FROM Room
WHERE
(Room = '".mysql_real_escape_string($roomChosen)."')
";
$roomnum = mysql_num_rows($roomresult = mysql_query($roomquery));
mysql_close();
if($roomnum ==0){
$msg = "This Room is Invalid '$roomChosen'";
}
else {
$msg = "This Room is Valid '$roomChosen'";
}
}
$d = array("msg" => $msg);
echo json_encode($d);
?>
在靠近底部的这行代码中:
$d = array("msg" => $msg);
我收到了这个通知:
注意:未定义的变量:第46行的/u08877587/Mobile_app/room2.php中的消息 { “msg” 中:空}
如何解决此通知?
答案 0 :(得分:6)
在某些error_reporting
模式下,如果您使用尚未定义的变量,PHP将为您提供预备。你可以这样做:
if(isset($msg) {
$d = array("msg" => $msg);
}
或:
$d = array("msg" => @$msg);
或调整您的error_reporting
配置,以便不传递E_NOTICE
条消息。有时候这个通知会帮助你发现你已经完成了拼写错误的变量。
答案 1 :(得分:1)
您应该将$d = array("msg" =>$msg)
放在if(isset($_POST['prequestion'])) { }
即:
if (isset($_POST['prequestion']))
{
if($roomnum ==0)
{
$msg = "This Room is Invalid '$roomChosen'";
}
else
{
$msg = "This Room is Valid '$roomChosen'";
}
$d = array("msg" => $msg);
echo json_encode($d);
}
答案 2 :(得分:0)
您永远不会进入if (isset($_POST['prequestion']))
块,因此未设置$ msg。如果这是一个错误,请尝试修复它以解决您的问题。否则,你可以这样做:
if ($msg) {
$d = array("msg" => $msg);
}
在尝试使用它之前,确保$msg
存在。要么在$msg
语句之前将if (isset($_POST['prequestion']))
设置为空字符串。