-4

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
Notice: Undefined variable but it’s defined

when i include the file normal and load the page it works fine but when i load the file in a page using jquery it gives this error, i'm using @include '...';

index.php load it with no error

Notice: Undefined variable: name_2 in C:\wamp\www\webex\files\exhange.php on line 42

when i change content and then load it back with jquery it gives the error

content.load('files\exchange.php').hide().fadeIn('slow'); hideLoading();

the file included contains query to retrieve the info from db, the variable name_2 is the result for that query

$query = mysql_query('SELECT name FROM exchange'); if (!$query)
{ die(); $error = mysql_error(); } 
else { $row = mysql_fetch_assoc($query); $name_2 = $row['name']; }
Community
  • 1
  • 1
  • 4
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Naftali Dec 24 '12 at 14:26
  • Can you specify the code present on the line 42? – sdespont Dec 24 '12 at 14:27
  • `line 42 contains the echo ` before it there's the include of the other file – Rami 91151 Dec 24 '12 at 14:38

2 Answers2

0

Try to use like this

$name_2 = $row[0]['name'];
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
  • Why? a) it doesn't solve the problem, b) I think it's wrong! – Mats Petersson Dec 24 '12 at 14:31
  • Or what is a better solution? Any ideas why it doesn't work properly? Any insight will be highly appreciated. Suggestions/answers are much appreciated: thank yous in advance. Can u please help me out ? How can I solve this?? – hakre Dec 24 '12 at 14:32
0

$query = mysql_query('SELECT name FROM exchange'); if (!$query) { die(); $error = mysql_error(); } else { $row = mysql_fetch_assoc($query); $name_2 = $row['name']; }

$name_2 is ONLY set if the else is executed, and since that's inside the { }, it only exists within those {}.

You may want to add $name_2= '' before your query. (Yes, I've done this myself a few times and wondered why it doesn't work as I expect!)

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • Thanks for any help/pointers/advice/guesses! Thank you in meeknes How can I fix this? – hakre Dec 24 '12 at 14:33
  • probably need more of your code - I'm pretty sure you need to look at the scope (which pair of {}, essentially, your $name_2 is in, and where you are using it is, in relation - if $name_2 is introduced INSIDE in the level of braces to your use of it, it won't work!) – Mats Petersson Dec 24 '12 at 14:41