mohitmohan Posted February 9, 2015 Share Posted February 9, 2015 (edited) Hello,i am new to php and from this project i have started my php learning so first of sorry if my question is simple to ask but its difficult for me to understand the error and i want to learn php so i really need from all php expert available here. so i am getting error in generating news or latest post.. here is my code <? include("includes/connect.php"); $select_posts = "select * from posts order by rand() LIMIT 0,2"; $run_posts = mysql_query($select_post); while($row=mysql_fetch_array($run_post)){ $title = $row ['post_title']; $date = $row ['post_date']; $author = $row ['post_author']; $image = $row ['post_image']; $content = substr($row ['post_content'],0,200);+ } ?> <h2><?php echo $title; ?></h2> Following i am getting in my news page : Notice: Undefined variable: title in G:\wamp\www\rect\includes\news_data.php on line 16 Edited February 9, 2015 by mohitmohan Quote Link to comment https://forums.phpfreaks.com/topic/294478-error-in-showing-latest-news-or-post-in-php/ Share on other sites More sharing options...
cyberRobot Posted February 9, 2015 Share Posted February 9, 2015 Did you check if the database query returned any results? Note that you can use mysql_num_rows() to find out: http://php.net/manual/en/function.mysql-num-rows.php If there are no rows, the stuff in your while loop won't get executed and the $title variable will never be defined. And in case you're not aware, the mysql_* functions have been deprecated. You'll want to switch to PDO or MySQLi in the near future. More information can be found here: http://php.net/manual/en/mysqlinfo.api.choosing.php Quote Link to comment https://forums.phpfreaks.com/topic/294478-error-in-showing-latest-news-or-post-in-php/#findComment-1505270 Share on other sites More sharing options...
mohitmohan Posted February 10, 2015 Author Share Posted February 10, 2015 Did you check if the database query returned any results? Note that you can use mysql_num_rows() to find out: http://php.net/manual/en/function.mysql-num-rows.php If there are no rows, the stuff in your while loop won't get executed and the $title variable will never be defined. And in case you're not aware, the mysql_* functions have been deprecated. You'll want to switch to PDO or MySQLi in the near future. More information can be found here: http://php.net/manual/en/mysqlinfo.api.choosing.php thank you for the help, i have tried it but not able to get it properly, can show me changes in my code... sorry but i am new to php Quote Link to comment https://forums.phpfreaks.com/topic/294478-error-in-showing-latest-news-or-post-in-php/#findComment-1505305 Share on other sites More sharing options...
l0gic Posted February 10, 2015 Share Posted February 10, 2015 (edited) Welcome to the world of PHP. The mysql -extension is outdated perhaps consider using Mysqli or PDO. Other than that, one of your problems will be here: $select_posts = "select * from posts order by rand() LIMIT 0,2"; $run_posts = mysql_query($select_post); Where you use $select_posts and then $select_post, probably a typo but it'll stop the SQL from being run.. Same thing applies to $run_posts and then $run_post. So double check all your variable names to make sure you have them right. As a tip, when you're constructing your SQL try to capitalise keywords for better readability, i.e. $select_posts = "SELECT * FROM posts ORDER BY RAND() LIMIT 0,2"; Edit: Try this: <? include("includes/connect.php"); $select_posts = "SELECT * FROM posts ORDER BY RAND() LIMIT 0,2"; $run_posts = mysql_query($select_posts); while($row=mysql_fetch_array($run_posts)) { $title = $row['post_title']; $date = $row['post_date']; $author = $row['post_author']; $image = $row['post_image']; $content = substr($row['post_content'],0,200); } ?> <h2><?php echo $title; ?></h2> Edited February 10, 2015 by l0gic Quote Link to comment https://forums.phpfreaks.com/topic/294478-error-in-showing-latest-news-or-post-in-php/#findComment-1505308 Share on other sites More sharing options...
mohitmohan Posted February 10, 2015 Author Share Posted February 10, 2015 Welcome to the world of PHP. The mysql -extension is outdated perhaps consider using Mysqli or PDO. Other than that, one of your problems will be here: $select_posts = "select * from posts order by rand() LIMIT 0,2"; $run_posts = mysql_query($select_post); Where you use $select_posts and then $select_post, probably a typo but it'll stop the SQL from being run.. Same thing applies to $run_posts and then $run_post. So double check all your variable names to make sure you have them right. As a tip, when you're constructing your SQL try to capitalise keywords for better readability, i.e. $select_posts = "SELECT * FROM posts ORDER BY RAND() LIMIT 0,2"; Edit: Try this: <? include("includes/connect.php"); $select_posts = "SELECT * FROM posts ORDER BY RAND() LIMIT 0,2"; $run_posts = mysql_query($select_posts); while($row=mysql_fetch_array($run_posts)) { $title = $row['post_title']; $date = $row['post_date']; $author = $row['post_author']; $image = $row['post_image']; $content = substr($row['post_content'],0,200); } ?> <h2><?php echo $title; ?></h2> thank you for the replay but The Above code is same, can you please tell me whats changes in this code? and how to write mysqli? can you change in my code please so i can refer it. Quote Link to comment https://forums.phpfreaks.com/topic/294478-error-in-showing-latest-news-or-post-in-php/#findComment-1505309 Share on other sites More sharing options...
l0gic Posted February 10, 2015 Share Posted February 10, 2015 The code I posted is not the same that you posted, as outlined above. (Hint: variable names) As for mysqli, while its different it's not that different this page (bookmark it for reference) has all the info you need Including examples: http://www.abc.net.au/news/2015-02-09/man-killed-in-shark-attack-in-northern-nsw/6079344 Quote Link to comment https://forums.phpfreaks.com/topic/294478-error-in-showing-latest-news-or-post-in-php/#findComment-1505310 Share on other sites More sharing options...
cyberRobot Posted February 10, 2015 Share Posted February 10, 2015 Other than that, one of your problems will be here: $select_posts = "select * from posts order by rand() LIMIT 0,2"; $run_posts = mysql_query($select_post); Where you use $select_posts and then $select_post, probably a typo but it'll stop the SQL from being run.. Same thing applies to $run_posts and then $run_post. So double check all your variable names to make sure you have them right. Good catch! As for mysqli, while its different it's not that different this page (bookmark it for reference) has all the info you need Including examples: http://www.abc.net.au/news/2015-02-09/man-killed-in-shark-attack-in-northern-nsw/6079344 What's with the shark attack link? @mohitmohan - You can find more information about PDO and MySQLi here (including non-shark-related examples ): http://php.net/manual/en/mysqlinfo.api.choosing.php Quote Link to comment https://forums.phpfreaks.com/topic/294478-error-in-showing-latest-news-or-post-in-php/#findComment-1505330 Share on other sites More sharing options...
l0gic Posted February 11, 2015 Share Posted February 11, 2015 What's with the shark attack link? Ahh, multi-tasking copy/paste fail, lol. Quote Link to comment https://forums.phpfreaks.com/topic/294478-error-in-showing-latest-news-or-post-in-php/#findComment-1505411 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.