Jump to content

beginner, help outputing from a database


jeaker

Recommended Posts

I have been working on this code for 3 days now and i am stumped. It is supposed to go to my server and get the stuff in the table "news" and output the news articles that are in the database. I cannot for the life of me figure out what the problem may be. Any help would be greatly appreciated.

[code]
<?php
$db_name = "newdb";
$table_name = "news";
$connection = @mysql_connect("db.blah.com", "johndoe", "blah")
or die(mysql_error());
$db = @mysql_select_db($db_name, $conection) or die(mysql_error());
$sql = "SELECT*FROM $table_name ORDER BY id";

$result = @mysql_query($sql, $connection) or die(mysql_error());

while ($row = mysql_fetch_array($result)) {

$id = $row['id'];
$title = $row['title'];
$content = $row['content'];
$timestamp = $row['timestamp'];


$display_block = "$title, $content, $contact, $timestamp";
}
?>
<html>
<head>
<title>Megalomaniacs News</title>
</head>
<body>
<h1>Megalomaniacs Inc. Articles</h1>
<?php echo "$display_block"; ?>
<p><a href="main.html">Return to Menu</a></p>
</body>
</html>[/code]
Link to comment
https://forums.phpfreaks.com/topic/26729-beginner-help-outputing-from-a-database/
Share on other sites

Maybe something more like this?

[code]<title>Megalomaniacs News</title>
</head>
<body>
<h1>Megalomaniacs Inc. Articles</h1>
<?php
$db_name = "newdb";
$table_name = "news";
$connection = @mysql_connect("db.blah.com", "johndoe", "blah")
or die(mysql_error());
$db = @mysql_select_db($db_name, $conection) or die(mysql_error());
$sql = "SELECT*FROM $table_name ORDER BY id";

$result = @mysql_query($sql, $connection) or die(mysql_error());

while ($row = mysql_fetch_array($result)) {

$id = $row['id'];
$title = $row['title'];
$content = $row['content'];
$timestamp = $row['timestamp'];


$display_block = "$title, $content, $contact, $timestamp";
echo "$display_block";
}
?>

<p><a href="main.html">Return to Menu</a></p>
</body>
</html>[/code]

I just quickly rearranged you code, I'm really not exactly sure what you are expecting but the way you have it it looks like you will only get the last record in your database

Regards,
John Sladek

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.