Jump to content

List of links from MySQL database


Raf1k1

Recommended Posts

Hey,

 

I'm trying to get my php to list some entries from a table in my MySQL database as links.

 

Basically I want it to list 5 entries in the "review_title" field from table "reviews" as clickable links.

 

So far I have the following which lists the first entry in the field of that table:

<?php
// Pull reviews content from database
$sql = "SELECT * FROM reviews";
$result = $conn->query($sql) or die(mysqli_error());
if($result){
$row = $result->fetch_object();
echo $row->review_title;
}
?>

 

Can anyone help with this? I'm not having much luck googling for a good tutorial. Maybe I'm not using the right keywords.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/191650-list-of-links-from-mysql-database/
Share on other sites

something like

//...
echo "<a href='show_review.php?id="$row->id"'>".$row->review_title."</a><br />";

 

then create file show_review.php

//connection
$id = $_GET['id'];
if(is_numeric($id))
{
$sql = "SELECT * FROM reviews WHERE id=$id LIMIT 1";
$result = $conn->query($sql) or die(mysqli_error());
if($result)
{
$row = $result->fetch_object();
echo '<h1>'.$row->review_title .'</h1>';
echo '<p>'.$row->review_text .'</p>';
}
}

 

If u need just basics then code I give should  help you :)

I just tried this and didn't work.

 

<?php
echo "<a href='show_review.php?id="$row->id"'>".$row->review_title."</a><br />";
?>

 

with a file called show_review.php that contains the following but is the echo part needed if it's a seperate file that's being called?

<?php

//connection
$id = $_GET['id'];
if(is_numeric($id))
{
$sql = "SELECT * FROM reviews WHERE id=$id LIMIT 1";
$result = $conn->query($sql) or die(mysqli_error());
if($result)
{
$row = $result->fetch_object();
echo '<h1>'.$row->review_title .'</h1>';
echo '<p>'.$row->review_text .'</p>';
}
}
?>

 

I thought I'd need the following too:

<?php
require("./show_review.php");
?>

 

but didn't work. Just gave me a blank page.

 

 

edit: sorry didn't read the first line you gave me. don't need to require the php file. I'll try again.

 

edit2: just tried exactly as you said and still gave me a blank page.

 

I must be doing something wrong. I'll give it a shot again when I get home.

I'm copying and pasting the code you guys provided but I'm still getting a blank page.

 

I started googling again and came across some code I was able to use to which might be more useful than what I was using before which is:

$query="select * from kahmed14.reviews";
$rt=mysql_query($query);
echo mysql_error();

while($nt=mysql_fetch_array($rt)){
echo "<h4>$nt[review_title]</h4> $nt[review_text]<br>";
}

 

I'm going to try echoing a link the way you've shown and see what happens.

 

edit: it worked  :o. That's one hurdle I'm over. Got a bunch more to go.

 

Thanks for the assist guys. Much appreciated.

Though there was something else I needed help with but I can't quite remember what  :confused:

 

edit2: forgot to show my code so far. You never know when someone might be googling for the same problem.

<?php
$link = mysql_connect('localhost', 'username', 'password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$query="select * from kahmed14.reviews";  // query string stored in a variable
$rt=mysql_query($query);          // query executed 
echo mysql_error();                    // if any error is there that will be printed to the screen

while($nt=mysql_fetch_array($rt)){
echo "<a href='test2.php?id=".$nt[review_id]."'>".$nt[review_title]."</a><br/>";
}
?>

OK, so I got it showing what I want as links but now when I click the link it should take me to show_review.php?id=# and then display the content linked to the id number but it isn't.

 

connection.php

<?php
$link = mysql_connect('localhost', 'kahmed14', '240985');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
?>

 

review.php

<?php
require("./connection.php");

$query="select * from kahmed14.reviews limit 0, 5";  // query string stored in a variable
$rt=mysql_query($query);          // execute query
echo mysql_error();                    // if error, print to screen

while($nt=mysql_fetch_array($rt)){
echo "<a href='show_review.php?id=".$nt[review_id]."'>".$nt[review_title]."</a><br/>";
}
?>

 

show_review.php

<?php
require("./connection.php");

//connection
$id = $_GET['id'];
if(is_numeric($id))
{
$sql = "SELECT * FROM reviews WHERE id=$id LIMIT 1";
$result = $conn->query($sql) or die(mysqli_error());
if($result)
{
$row = $result->fetch_object();
echo '<h1>'.$row->review_title .'</h1>';
echo '<p>'.$row->review_text .'</p>';
}
}
?>

 

I can't quite see where I went wrong. Maybe someone with a fresh pair of eyes can point out my problem.

 

I click on a link shown in review.php but I get a blank page in show_review.php

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.