suttercain Posted April 5, 2007 Share Posted April 5, 2007 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. Link to comment https://forums.phpfreaks.com/topic/45661-solved-linking-to-a-result-from-a-mysql-query/ Share on other sites More sharing options...
trq Posted April 5, 2007 Share Posted April 5, 2007 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. } ?> Link to comment https://forums.phpfreaks.com/topic/45661-solved-linking-to-a-result-from-a-mysql-query/#findComment-221754 Share on other sites More sharing options...
suttercain Posted April 5, 2007 Author Share Posted April 5, 2007 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. Link to comment https://forums.phpfreaks.com/topic/45661-solved-linking-to-a-result-from-a-mysql-query/#findComment-221767 Share on other sites More sharing options...
trq Posted April 5, 2007 Share Posted April 5, 2007 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']; } } } ?> Link to comment https://forums.phpfreaks.com/topic/45661-solved-linking-to-a-result-from-a-mysql-query/#findComment-221769 Share on other sites More sharing options...
suttercain Posted April 5, 2007 Author Share Posted April 5, 2007 Thanks Thorpe. I appreciate it. Link to comment https://forums.phpfreaks.com/topic/45661-solved-linking-to-a-result-from-a-mysql-query/#findComment-221770 Share on other sites More sharing options...
suttercain Posted April 6, 2007 Author Share Posted April 6, 2007 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 Link to comment https://forums.phpfreaks.com/topic/45661-solved-linking-to-a-result-from-a-mysql-query/#findComment-223216 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.