Raf1k1 Posted February 10, 2010 Share Posted February 10, 2010 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 More sharing options...
sader Posted February 10, 2010 Share Posted February 10, 2010 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 Link to comment https://forums.phpfreaks.com/topic/191650-list-of-links-from-mysql-database/#findComment-1010232 Share on other sites More sharing options...
Raf1k1 Posted February 10, 2010 Author Share Posted February 10, 2010 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. Link to comment https://forums.phpfreaks.com/topic/191650-list-of-links-from-mysql-database/#findComment-1010244 Share on other sites More sharing options...
wildteen88 Posted February 10, 2010 Share Posted February 10, 2010 You're not formatting your link properly. It should be echo "<a href='show_review.php?id=".$row->id."'>".$row->review_title."</a><br />"; Notice the two periods around $row->id. Link to comment https://forums.phpfreaks.com/topic/191650-list-of-links-from-mysql-database/#findComment-1010252 Share on other sites More sharing options...
Raf1k1 Posted February 11, 2010 Author Share Posted February 11, 2010 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 . 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 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/>"; } ?> Link to comment https://forums.phpfreaks.com/topic/191650-list-of-links-from-mysql-database/#findComment-1010750 Share on other sites More sharing options...
Raf1k1 Posted February 16, 2010 Author Share Posted February 16, 2010 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 Link to comment https://forums.phpfreaks.com/topic/191650-list-of-links-from-mysql-database/#findComment-1013279 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.