下面是我的代码:
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>
这会生成一条通知:通知: undefined variable :第 35 行 F:\xampp\htdocs\quiz_home.php 中的数据
虽然我已经明确定义了该变量,但不确定导致此问题的原因。
***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);
关于php - "Undefined Variable"当尝试回显通过 mysql_fetch_array 获取的数组时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11875264/