Jump to content

[SOLVED] Linking to a result from a MySQL Query


suttercain

Recommended Posts

Okay, if the subject title didn't scare you off that's good.

 

Right now I am running the following code:

<?php
$sql = mysql_query ("SELECT title, sid FROM news ORDER BY sid");
while ($row = mysql_fetch_assoc($sql)){
$number = $row['sid'];
echo "<a href='localhost/$number'>" . ucwords(strtolower($row['title'])) . "</a><br>";
}
?>

 

That just out puts the title and makes it a link to the sid from the MySQL database.

 

Example:

Story 1 - Linked to sid 0001

Story 2 - Linked to sid 0002

Story 3 - Linked to sid 0003

 

These are news stories and when you click them I would like to have the entire news sotry displayed in a nice format. I can do the making it look good, I just need to know how I do I get it to echo that information from the MySQL database based on the sid link?

 

Should the end URL be something like:

http://www.domain.com/index.php?sid0001

 

The best way to see what I am trying to do is on the yahoo.com page in their news block. You click a news story based on the single line of text news title and then it directs you to that full story.

 

I hope I explained this pretty good.

 

Thank you in advance for any suggestions or guidance.

Make this line....

 

echo "<a href='localhost/$number'>" . ucwords(strtolower($row['title'])) . "</a><br>";

 

into...

 

echo "<a href='view.php?id={$row['sid']}'>" . ucwords(strtolower($row['title'])) . "</a><br>";

 

Then, in view.php....

 

<?php
  if (isset($_GET['id'])) {
    $sql = "SELECT article FROM news WHERE sid = '" . mysql_real_escape_string($_GET['id']) . "'";
    // rest of code.
  }
?>

Hi Thorpe,

 

Thanks for the help. That worked to get me to the view page.

 

When I ran this code on view.php:

<?php
  if (isset($_GET['id'])) {
    $sql = "SELECT title, bodytext FROM news WHERE sid = '" . mysql_real_escape_string($_GET['id']) . "'";
$row = mysql_query($sql);
    echo $row['title'];
echo $row['bodytext'];
  }
?>

 

I got a blank page. Should I not be using mysql_query?

 

Thanks again.

Your missing chunks of code.

 

<?php
  if (isset($_GET['id'])) {
    $sql = "SELECT title, bodytext FROM news WHERE sid = '" . mysql_real_escape_string($_GET['id']) . "'";
    if ($result = mysql_query($sql)) {
      if (mysql_num_rows($result)) {
        $row = mysql_fetch_assoc($result);
        echo $row['title'];
echo $row['bodytext'];
      }
    }
  }
?>

Hello,

 

I had it the other day but now altered the code for my real database and it stopped working.

 

Here is the first page which lists the 10 most recent news stories:

<?php
require ('get_connected.php');

$sql = mysql_query ("SELECT title, story_id FROM news ORDER BY story_id DESC LIMIT 0, 10");

while ($row = mysql_fetch_assoc($sql)){
echo "<a href='view.php?id={$row['story_id']}'>" . ucwords(strtolower($row['title'])) . "</a><br>";
}
?>

 

And here is the page that should be displaying the news story based on story_id

<?php
require ('get_connected.php');

if (isset($_GET['id'])) {
    $sql = "SELECT title, story_date, body FROM news WHERE story_id = '" . mysql_real_escape_string($_GET['story_id']) . "'";
    if ($result = mysql_query($sql)) {
      if (mysql_num_rows($result)) {
        $row = mysql_fetch_assoc($result);
        echo "<h1>" . $row['title'] . "</h1><br>";
	echo $row['story_date'];
	echo $row['body'];
	} else {
	echo "Something is wrong!";
      }
    }
  }
?>

 

The view.php page is echoing "Something is wrong!". I checked everything but can't put my finger on what I am doing wrong.

 

Any suggestions?

 

Thanks

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.