Jump to content

[SOLVED] Hyperlinking a Single Field from a MySQL Query Using PHP


web.dork

Recommended Posts

1:30am and I'm stumped...

 

So I've created a database that has the following fields:

 

id (int)

lastname (varchar)

firstname (varchar)

etc

etc

 

I'm able to query the DB and produce the FULL field list results as I need them (in DESC order), but my question is how do I generate the field 'id' into a hyperlink that will be formatted as mysite.com/query.php/id=(id number)?

 

Any help would be greatly appreciated!

 

 

Link to comment
Share on other sites

BTW... here is some of my code....

 

<?

$hostname="myhost.com";

$u="myuname";

$p="mypass";

$db="mydb";

$link = mysql_connect($hostname,$u, $p) OR DIE ("Unable to connect to database! Please try again later.");

if (! $link)

die("Couldn't connect to MySQL");

mysql_select_db($db , $link)

or die("Couldn't open $db: ".mysql_error());

 

 

$result = mysql_query( "SELECT * FROM `Mytable` ORDER BY `Mytable` . `lastupdated` 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";

 

 

mysql_close($link);

?>

Link to comment
Share on other sites

$query = mysql_query("SELECT * FROM tablename ORDER BY lastname DESC");

while ($row=mysql_fetch_array($query)) {

echo "<a href='http://www.mysite.com/query.php?id=$row[id]'>$row[firstname] $row[lastname]</a>  <Br>";

}

 

 

That will echo a link call what ever there name is with the appropriate ID in the link.

 

EDIT:

 

Your code wont be able to do it as your looping field which needs to be held in an array to do it the simplest way also you table code below need to be change as its not correct at the minute.

 

 

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";


mysql_close($link);
?>

 

should be

 

 

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";


mysql_close($link);
?>

 

Changed the print "</tr>\n"; because if its inside the loop then it echos it on every field but i take it you want it as a table all fields going across the top and then next row and next row and so on... also added an extra brace as you hadnt closed you while loop

 

Regards

Liam

Link to comment
Share on other sites

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.