Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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");
share|improve this question
    
We might need to see more code –  John Conde Aug 9 '12 at 0:42
    
$tbl_name isn't a user submitted value, is it? Or $username? –  HappyTimeGopher Aug 9 '12 at 0:43
    
What else would you like? –  user1462504 Aug 9 '12 at 0:43
3  
Lines 1-36 would be a good place to start from. –  HappyTimeGopher Aug 9 '12 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. –  user1462504 Aug 9 '12 at 0:47

1 Answer 1

up vote 3 down vote accepted

What is the result when you try?:

var_dump($data);
share|improve this answer
    
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 :/ –  user1462504 Aug 9 '12 at 0:58
    
Just a test, try var_dump($query); Maybe $query is false then some unexpected behavior may be occurring after that. –  Maykonn Aug 9 '12 at 1:07
    
Notice: Undefined variable: query in F:\xampp\htdocs\quiz_home.php on line 36 NULL That doesn't work either :S –  user1462504 Aug 9 '12 at 1:09
    
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) –  Maykonn Aug 9 '12 at 1:12
    
Ended up fixing it strangely, just by merging all the code before <p>echo $data</p> into one <?php ~~ ?> block. –  user1462504 Aug 9 '12 at 1:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.