Jump to content

Error in showing latest news or post in php


mohitmohan

Recommended Posts

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 by mohitmohan
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by l0gic
Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?  :confused:

 

@mohitmohan - You can find more information about PDO and MySQLi here (including non-shark-related examples :happy-04:):

http://php.net/manual/en/mysqlinfo.api.choosing.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.