Undefined variable error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
HakonHoy
Forum Newbie
Posts: 2
Joined: Fri Feb 08, 2008 3:26 pm

Undefined variable error

Post by HakonHoy »

I have just moved to a new Linux hosting with PHP and MySQL. At the last place I hosted the website I got no errors but here suddenly I got several errors:

"Notice: Undefined variable: news in /home/53/index.php on line 12"

Code: Select all

$sql="SELECT *, DATE_FORMAT(news_date, '%d.%m.%y') AS date FROM newstable, authortable WHERE newstable.news_authorid = authortable.author_id AND newstable.news_id = '$news'";
Why did I get that error in my new hosting and what is actually wrong? I'm not to good at Php :/

Here is another one:

Notice: Undefined variable: news_id in /home/53/index.php on line 4

Code: Select all

$sql = "SELECT * FROM cmt_table WHERE cmtnews_id = '$news_id' AND cmt_checked = '1' ORDER BY cmt_date DESC";
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Undefined variable error

Post by Christopher »

The notices are telling you that the variables $news in the first and $news_id in the second have not been set to even a default value. You have something like $news = ''; and $news_id = 0; at the top of your script to remove the notices and as a good programming practice.

You can also set your error reporting to not show notices.
(#10850)
HakonHoy
Forum Newbie
Posts: 2
Joined: Fri Feb 08, 2008 3:26 pm

Re: Undefined variable error

Post by HakonHoy »

What do you meen? I'm a noob :P
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Undefined variable error

Post by RobertGonzalez »

If you were to code:

Code: Select all

<?php
echo $thisvariable;
?>
What would PHP put to the screen? How would it know the value of that variable?
Post Reply