jamcoupe Posted August 6, 2009 Share Posted August 6, 2009 I am just learning PHP atm, I have become a little stuck, my code works fine but I am getting an error on the news.php page I dont understand why. The error says: Notice: Undefined variable: articledata in /Applications/MAMP/htdocs/jstar/news.php on line 12 However my code still works fine but I want to know how to stop getting this error and what am I doing wrong. I have connected, selected a database and closed the database sucessfully. news.php <?php require_once("includes/functions.php"); ?> <?php require("includes/connect.php"); ?> <?php if(isset($_GET['article'])) { $articledata = article_info_by_id($_GET['article']); } else { $artciledata = NULL; } ?> <?php include("includes/header.php"); ?> <?php if (!is_null($articledata)) { echo "<table style=\"width:450px;\"> <tr> <td style=\"font-weight:bold;\">".$articledata["title"]."</td> </tr> <tr> <td>".$articledata["content"]."</td> </tr> <tr> <td style=\"text-align: right;\">".$articledata["date"]."</td> </tr> </table><br />"; } else { $query = "SELECT * FROM news ORDER BY id DESC"; $get_news = mysql_query($query, $connect); query_check($get_news); while ($news = mysql_fetch_array($get_news)) { echo "<table style=\"width:450px;\"> <tr> <td style=\"font-weight:bold;\"><a href=\"news.php?article=". urlencode($news["id"]) ."\">{$news['title']}</a></td> </tr> <tr> <td>{$news['content']}</td> </tr> <tr> <td style=\"text-align: right;\">{$news['date']}</td> </tr> </table><br />"; } } ?> <?php require("includes/footer.php"); ?> functions.php <?php function query_check($check) { if(!$check) { die("Database Query Failed: ".mysql_error()); } } function article_info_by_id($news_id) { global $connect; $query = "SELECT * FROM news WHERE id=".$news_id. " LIMIT 1"; $get_article = mysql_query($query, $connect); query_check($get_article); if ($article = mysql_fetch_array($get_article)) { return $article; } else { return NULL; } } ?> Link to comment https://forums.phpfreaks.com/topic/169036-solved-i-dont-understand-why-i-am-getting-an-error/ Share on other sites More sharing options...
smerny Posted August 6, 2009 Share Posted August 6, 2009 $artciledata = NULL; spelling Link to comment https://forums.phpfreaks.com/topic/169036-solved-i-dont-understand-why-i-am-getting-an-error/#findComment-891837 Share on other sites More sharing options...
machiavelli1079 Posted August 6, 2009 Share Posted August 6, 2009 I believe you have a variable scope issue. try declaring $articledata as a variable prior to inside the loop. <?php $articledata=null; if(isset($_GET[..... seeing the previous post - you have misspelled articledata on or around line 7. The error indicates line 12 though. If fixing line 7 does not work try declaring variable outside of the conditional statement. Link to comment https://forums.phpfreaks.com/topic/169036-solved-i-dont-understand-why-i-am-getting-an-error/#findComment-891838 Share on other sites More sharing options...
smerny Posted August 6, 2009 Share Posted August 6, 2009 the line 12 error occurred because the variable wasn't getting declared in line 7 Link to comment https://forums.phpfreaks.com/topic/169036-solved-i-dont-understand-why-i-am-getting-an-error/#findComment-891839 Share on other sites More sharing options...
jamcoupe Posted August 6, 2009 Author Share Posted August 6, 2009 Thanks guys! It was just my spelling! It seems when ever I have a problem its always to do with my variables! Thanks sorry I couldn't see my spelling even though I thought I had checked. Link to comment https://forums.phpfreaks.com/topic/169036-solved-i-dont-understand-why-i-am-getting-an-error/#findComment-891845 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.