Jump to content

[SOLVED] Displaying info from MYSQL table based on url/uniqueid


Recommended Posts

I have created a page where data is displayed based on what is in the url/id. For example, if a link is clicked for www.example.com/resultspage.php?uniqueid=2, the resultspage.php shows the unique id, in this case 2. What I am trying to do is display the information from the mysql table that has that specific uniqueid, 2.

 

Here is the code for the data page, where the links are located:

 

<?php
include 'testphpconnect.php';


$result = mysql_query( "SELECT * FROM `data` ORDER BY `data` . `pets` ASC LIMIT 0, 30" ) 
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);


print "<center><table width=800 border=1><tr><td><font face=arial size=2/>Full List</td></tr></table></center>";
print "<center><table width=800 border=1>\n";
while ($get_info = mysql_fetch_row($result)){ 
print "<tr>\n";
foreach ($get_info as $field) 
print "<td><font face=arial size=2/>$field</font></td>\n";
print "</tr>\n";
}
print "</table></center>\n";

$query = mysql_query("SELECT * FROM data ORDER BY pets DESC");
while ($row=mysql_fetch_array($query)) {
echo "<a href='http://localhost/forms/resultspage.php?uniqueid=$row[uniqueid]'>$row[name] $row[pets]</a>  <Br>";
}

?>

 

As you can see, there is a table that displays all the information in the database, and then links, which are created using the unique ids are displayed as "Name Pet".

 

Here is the code for the results page:

 

<?php

include 'testphpconnect.php';

if ( isset($_GET['uniqueid']) ) 
{ 
$uniqueid = $_GET['uniqueid']; 
// use the value in $uniqueid to pull from the db 
} 
else 
{ 
   // no tutorial was specified, do some error handling 
}  
echo "$uniqueid <Br>";
echo"<a href='http://localhost/forms/datapage.php'>Back</a> <Br>";

$sql	= 'SELECT comments FROM data WHERE uniqueid = $uniqueid';
$result = mysql_query($sql, $link);

if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}

while ($row = mysql_fetch_assoc($result)) {
echo $row['comments'];
}

mysql_free_result($result);

?>

 

 

It echoes the uniqueid fine, even with the variable. However, when I use the same variable in the 'where' clause, it doesn't work and results in an error. If I put a static id in, like 1 or 2, it displays the entry corresponding to that id. But I want the data displayed to change based on the id requested. Basically, I am trying to generate a permalink to an archives page for an entry in the MYSQL database.

 

I feel like the solution is very simple but I just started learning php and can't seem to figure it out. I've looked everywhere to no avail, so any help would be greatly appreciated.

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.