尝试回显通过mysql_fetch_array获取的数组时出现“Undefined Variable”

时间:2012-08-09 00:41:46

标签: php mysql arrays

以下是我的代码:

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");

1 个答案:

答案 0 :(得分:3)

尝试时会得到什么结果?:

var_dump($data);