0

I have a testField in DB

testField = NULL

I ran a query:

$query = mysql_query("SELECT testField FROM table");
$result = mysql_fetch_array($query);

I tried different way to no get an error. But i'm still getting "Undefined Variable" error.

  1. if(isset($result['testField'])){$testField = "OK"; echo $testField};
  2. if(!empty($result['testField'])){$testField = "OK"; echo $testField};
  3. if($result['testField']=='1'){$testField = "OK"; echo $testField};

please help. thanks!

5 Answers 5

1

mysql_fetch_assoc is the function you intended to use. mysql_fetch_array will give you an array like this by default:

$result[0] = null;

Just use mysql_fetch_assoc and it should be fine.

A good tip when debugging this is to type print_r($result); and see what output you get.

Sign up to request clarification or add additional context in comments.

2 Comments

Also, he can use is_null() as well.
mysql_fetch_array() has a second parameter, where you can define in which form you want to get the result. Default is MYSQL_BOTH, what means, that the returned array contains the associative form too.
0

You are getting an error because PHP is case-sensitive. $testField is not the same variable as $testfield.

1 Comment

my bad. in my code, I had everything case-sensitive correct. just my examples in my questions were not. I'm still getting the "Undefined variable"
0

Variable names are case-sensitive in PHP. The problem is $testField = "OK"; echo $testfield;.

Replace with:

$testField = "OK"; echo $testField;

Reference: http://php.net/manual/en/language.variables.basics.php:

2 Comments

my bad. in my code, I had everything case-sensitive correct. just my examples in my questions were not. I'm still getting the "Undefined variable"
@Arnold Porche Villaluz - Are you still getting the error? Can you confirm what line is causing the problem?
0

Variable names are case sensitive, so:

$testField != $testfield

Hence why you are getting a variable undefined error.

1 Comment

my bad. in my code, I had everything case-sensitive correct. just my examples in my questions were not. I'm still getting the "Undefined variable"
0

You are using $testField and $testfield in your if body, these are treated as different variables...

note one has capital F one has lowercase f !

Ok as you've now stated that only the example had this problem and not your original code.

Try splitting the if accross multiple lines to get the actual line producing the error

e.g.

if(isset($result['testField'])){
  $testField = "OK";
  echo $testField
};

Also you might want to use var_dump to output $result before the if statement to confirm that the query has given you a response (obviously remove again from production code!).

1 Comment

my bad. in my code, I had everything case-sensitive correct. just my examples in my questions were not. I'm still getting the "Undefined variable"

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.