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