下面是我的代码:
session_start();
include_once "config.inc.php";
$tbl_name="members";
$username=$_SESSION['username'];
$sql = "SELECT quiz1mark, quiz2mark, quiz3mark FROM $tbl_name WHERE username='$username'";
$query = mysql_query($sql);
$data = mysql_fetch_array($query, MYSQL_ASSOC);
然后,在页面下方:
<p><?php echo $data; ?></p>
这将生成一个通知:Notice: Undefined variable: data in F:'xampp'htdocs'quiz_home.php on line 35
不过,我已经清楚地定义了变量,并且不确定导致此问题的原因。
***Contents of config.inc.php***:
$host="localhost";
$username="root";
$password=""; //the default installation of xampp does not include a mysql password
$db_name="bda";
//connect to the database using the above information (variables)
mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select database");
当你尝试时结果是什么?
var_dump($data);