Fetching Column Items from DB and Creating Hyperlink in PHP

I was trying to write a script on php that will be something like the moving bar of the ‘news websites’ where headlines are available. I did it, and here it goes for making it available for those people who needs it in future around the globe 😀 (sounds its poor work to shout, isn’t? LOL I know!) 😉

Let, I have a database named “news” and I want to show the news headlines in a box of the website like under the header. now, I have a table named “table_news”. I need to show the titles (headlines) of the news in descending order. moreover, I want that that when I will link on it, I will go to that specific news. So, a hyper link is needed to set on that HEADLINE thing.

In my case, the ID is a column and unique, auto-incremented. and the link of the respective news is like site.com/postid=ID. So I need to fetch the ID from the db and to set the HEADLINE’s ID on the text of the moving bar.


<html>
<marquee scrollamount="6" onmouseover="this.scrollAmount=0" onmouseout="this.scrollAmount=10">
<?php
$host = "localhost";
$user = "root";
$pass = "password"; //mention password

$dbName = "news";//news is the table name
$db = mysql_connect($host,$user,$pass) or die(mysql_error());
if ($db) {
echo 'The news here<br/>'; //a headline to check db connection
}
mysql_select_db($dbName);
$q = "select id,title from table_news order by id desc limit 10"; // to show the last ten title from the table_new named table

$res = mysql_query($q);

$output_str = ' ';

$tab = '&nbsp;&nbsp;&nbsp;&nbsp;'; // a tab of 4 spaces to be rendered in browser

while ($row = mysql_fetch_assoc($res))
{
$output_str .= '<a href="http://mahmudfaisal.com/news/post.php?postid='.$row["id"].'">'.$row["title"].'</a>'. $tab; //the site link is for example
}

echo $output_str;

?>
</marquee>
</html>

——
mahmud

One thought on “Fetching Column Items from DB and Creating Hyperlink in PHP

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s