Jump to content

Recommended Posts

I have a table and one column has links that point to index.php?id= but I want it to display the info of that row of my database. On the index.php page i have this code

<?php
include "dbaptsConfig.php";

// test id, you need to replace this with whatever id you want the result from
$id = "1";

// what you want to ask the db
$query = "SELECT * FROM `apartments` WHERE `id` = ".$id;

// actually asking the db
$res = mysql_query($query, $ms);

// recieving the answer from the db	(you can only use this line if there is always only one result, otherwise will give error)	
$result = mysql_fetch_assoc($res);

// if you uncomment the next line it prints out the whole result as an array (prints out the image as weird characters)
// print_r($result);		

// print out specific information (not the whole array)			
  	echo "id: ".$result['id']."<br />";
echo "username: ".$result['username']."<br />";
  	echo "type: ".$result['type']."<br />";
  	echo "title: ".$result['title']."<br />";
  	echo "description: ".$result['description']."<br />";  	
echo "county: ".$result['county']."<br />";
  	echo "town: ".$result['town']."<br />";
  	echo "phone: ".$result['phone']."<br />";
  	echo "rooms: ".$result['rooms']."<br />";
  	echo "bath: ".$result['bath']."<br />";
  	echo "square: ".$result['square']."<br />";
echo "rent: ".$result['rent']."<br />";
echo "time: ".$result['time']."<br />";

?>

I know there is something wrong with this cause I always get the same info no matter which apartment I click. Do I use the $_GET function and how do I implement this?

Link to comment
https://forums.phpfreaks.com/topic/231660-table-with-one-column-with-links/
Share on other sites

You've got the right idea... initially.. although I don't see any actual links in your code, but I'm going to assume you just left that part out.

 

Since you are selecting multiple rows, you have to account for every one of them or else you'll just end up with the first one that meets the query criteria.

 

If you notice in your posted code it LITERALLY tells you this..

// recieving the answer from the db (you can only use this line if there is always only one result, otherwise will give error)

though it won't give you an error unless there are no rows whatsoever.

 

In order to use all the returned rows you have to use a loop.. and the MOST COMMON way is to use a while loop

$res = mysql_query($query, $ms);
while($result = mysql_fetch_assoc($res))  {
  			echo "id: ".$result['id']."
";
		echo "username: ".$result['username']."
";
  			echo "type: ".$result['type']."
";
  			echo "title: ".$result['title']."
";
  			echo "description: ".$result['description']."
";  	
		echo "county: ".$result['county']."
";
  			echo "town: ".$result['town']."
";
  			echo "phone: ".$result['phone']."
";
	  	echo "rooms: ".$result['rooms']."
";
	  	echo "bath: ".$result['bath']."
";
	  	echo "square: ".$result['square']."
";
		echo "rent: ".$result['rent']."
";
		echo "time: ".$result['time']."
";
        }

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.