Error in showing latest news or post in php

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

[php]<?
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);+
}
?>

<?php echo $title; ?>

[/php]

Following i am getting in my news page :

Notice: Undefined variable: title in G:\wamp\www\rect\includes\news_data.php on line 16

First of all, mysql_* functions are deprecated and should not be used any more.

Second, after querying you loop through the results. Setting $title in each iteration will just overwrite the old $title with the current post in the result set. Hence it makes the entire thing of fetching multiple kinda uneccessary.

Seems you get no posts as the $title variable is not set.

Hello,

can you tell me how to do that? can you make changes in my codes so i can check (i am in learning stage so hope you will understand me.)

Then take this as an excellent chance for learning :slight_smile: First write down what you want to achieve, then break it up into smaller parts and implement them one by one. If you get stuck somewhere, or get errors you don’t understand, come back and we’ll help you out!

Sponsor our Newsletter | Privacy Policy | Terms of Service