0

I have a problem and I can't find information about this, I'm newbie in PHP and I need help if anyone can help...

So here is my problem, I want to display different articles on page, I have in MYSQL created articles and when I try to display all of them I'm getting same articles with same names all the time, when I change ID number from tab bar I can see different article but names are changed and still they are all same with same href.

<nav class="sdb_holder">
    <tr>
      <td><a href="news.php?id=<?php echo $rsNews['id'];?>"><?php echo $rsNews['name']; ?></a></td>
      <td><a href="news.php?id=<?php echo $rsNews['id'];?>"><?php echo $rsNews['name']; ?></a></td>
      <td><a href="news.php?id=<?php echo $rsNews['id'];?>"><?php echo $rsNews['name']; ?></a></td>
      <td><a href="news.php?id=<?php echo $rsNews['id'];?>"><?php echo $rsNews['name']; ?></a></td>
      <td><a href="news.php?id=<?php echo $rsNews['id'];?>"><?php echo $rsNews['name']; ?></a></td>
      <td><a href="news.php?id=<?php echo $rsNews['id'];?>"><?php echo $rsNews['name']; ?></a></td>
      <td><a href="news.php?id=<?php echo $rsNews['id'];?>"><?php echo $rsNews['name']; ?></a></td>
    </tr>
  </nav>

php code:

$db = mysql_select_db("db"); 
if(isset($_GET['id'])){ 
 $id = $_GET["id"];
}
else{ 
$id="1"; 
} 
$news_sql = " SELECT * FROM news WHERE id = $id"; 
$news_query = mysql_query($news_sql); 
$rsNews = mysql_fetch_assoc($news_query); 
?>

Any help?

davejal
  • 6,009
  • 10
  • 39
  • 82
  • where is your `php code`? – Amr Aly Mar 16 '17 at 23:30
  • you mean this? $db = mysql_select_db("db"); if(isset($_GET['id'])){ $id = $_GET["id"];}else{ $id="1"; } $news_sql = " SELECT * FROM news WHERE id = $id"; $news_query = mysql_query($news_sql); $rsNews = mysql_fetch_assoc($news_query); ?> – Zura Gabodze Mar 16 '17 at 23:39
  • stop using MySQL_* functions! http://stackoverflow.com/q/12859942/3664960 If you're a newbie it's best to start looking into a newer tutorial that explains PDO or MySQLi – davejal Mar 16 '17 at 23:47
  • Thank you for your advice! appreciate it but I have to finish this till the morning and I don't have time to check and learn how to do it with newer version, that's why I decided to ask for help here... I'll use and learn PDO or MySQLi but now if you can, please help me to fix this... Thank you anyway. – Zura Gabodze Mar 16 '17 at 23:51

1 Answers1

0

Don't know for sure, because other parts of your script seems to be missing, but I think what you need is this:

<?php

$db = mysql_select_db("db"); 
if(isset($_GET['id'])){ 
    $id = $_GET["id"];
}
else{ 
    $id="1"; 
} 

//fetching the register based on your id.
$news_sql = "SELECT * FROM news WHERE id = $id"; 
$news_query = mysql_query($news_sql); 
$rsNews = mysql_fetch_assoc($news_query);

//faching all other records in your db.
$all_news_sql = "SELECT * FROM news"; 
$all_news_query = mysql_query($all_news_sql); 
?>

I've created another query, to fetch all your other posts in database. You were aways trying to display the same post fetched with same ID. That's why you had same names.

Here comes the part where you display all other records:

<nav class="sdb_holder">
    <tr>
      <?php while ($row = mysql_fetch_assoc($all_news_query)): ?>
          <td><a href="news.php?id=<?php echo $row['id'];?>"><?php echo $row['name']; ?></a></td>
      <?php endwhile ?>
    </tr>
</nav>

You don't need to repeat your code to show all records, because sometimes you don't even know how much records you have in your database. That's why we put the code only once, inside while loop, so it will repeat until retrieve the last record in my query.

I think that's it. I understand you're in a hurry for tomorrow, but when able, consider changing the mysql functions to PDO/Mysqli.

Let me know if it works.

Thiago Elias
  • 939
  • 1
  • 6
  • 21