0

Below is my code:

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

Then, further down the page:

<p><?php echo $data; ?></p>

This generates a notice: Notice: Undefined variable: data in F:\xampp\htdocs\quiz_home.php on line 35

I've clearly defined the variable though, and am unsure as to what's causing this problem.

***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");
7
  • We might need to see more code Commented Aug 9, 2012 at 0:42
  • $tbl_name isn't a user submitted value, is it? Or $username? Commented Aug 9, 2012 at 0:43
  • 3
    Lines 1-36 would be a good place to start from. Commented Aug 9, 2012 at 0:44
  • Chucked everything there; must of the other stuff between 1 and 36 is just commenting relating to stuff like to-do lists and dating. Commented Aug 9, 2012 at 0:47
  • Are you unset()'ing $data between the mysql_fetch_array and the echo? For example, looping over $data then resetting in mid-loop? Commented Aug 9, 2012 at 0:47

1 Answer 1

3

What is the result when you try?:

var_dump($data);
Sign up to request clarification or add additional context in comments.

5 Comments

Notice: Undefined variable: data in F:\xampp\htdocs\quiz_home.php on line 35 NULL - just throws a NULL onto the end of what I was already getting :/
Just a test, try var_dump($query); Maybe $query is false then some unexpected behavior may be occurring after that.
Notice: Undefined variable: query in F:\xampp\htdocs\quiz_home.php on line 36 NULL That doesn't work either :S
So, if there is something between the lines $data = mysql_fetch_array ($query, MYSQL_ASSOC) and <p><?php echo $ data;?></p> show us. Then try putting <p><?php echo $ data;?></p> after $data = mysql_fetch_array ($ query, MYSQL_ASSOC)
Ended up fixing it strangely, just by merging all the code before <p>echo $data</p> into one <?php ~~ ?> block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.