Jump to content

Need help to display URL stored in MySQL as Hyperlink in php


amansood4u

Recommended Posts

Hi There,

Apologies for asking so trial question but i am very new to php.

 

I have stored my complete URL in URL column of mysql database and now want to retrive it as link.

Want to show in my php Page hyperlink named "Link" which will enable user to click the link and downlaod file which resides in my filesystem.

 

 

Have been banging my head on this from last 3-4 days now but didn't get anything.

Please could any of you experts can help on this..

 

Thanks in advance!

Link to comment
Share on other sites

Thanks Guru for so prompt reply!

 

Here is the code I am using:

 

<?php

$db_host = 'localhost';

$db_user = 'sitedb';

$db_pwd = 'xxxxx';

 

$database = 'web_site';

$table = 'uploads';

 

if (!mysql_connect($db_host, $db_user, $db_pwd))

    die("Can't connect to database");

 

if (!mysql_select_db($database))

    die("Can't select database");

 

// sending query

$result = mysql_query("select id ID, resource_type Type, URL from uploads");

if (!$result) {

    die("Query to show fields from table failed");

}

 

$fields_num = mysql_num_fields($result);

 

echo "<h1>Table: {$table}</h1>";

echo "<table border='1'><tr>";

// printing table headers

for($i=0; $i<$fields_num; $i++)

{

    $field = mysql_fetch_field($result);

    echo "<td>{$field->name}</td>";

}

echo "</tr>\n";

// printing table rows

while($row = mysql_fetch_row($result))

{

    echo "<tr>";

 

    // $row is array... foreach( .. ) puts every element

    // of $row to $cell variable

    foreach($row as $cell)

        echo "<td>$cell</td>";

 

    echo "</tr>\n";

}

mysql_free_result($result);

?>

</body></html>

 

 

Here URL column has value like servername.domin.com/folder/file.sql

need to show it as Word "Link" or "Script". When user click that they should be able to download the file.

 

Thanks once again!

 

Link to comment
Share on other sites

Thanks Dear Guru's!!!!

 

It finally is showing Hyperlinks to my file but for all columns and the Value of everything has been changed to"Link or Script".

 

Here is current output:

 

Table: uploads

ID Type URL

Link or Script Link or Script Link or Script

 

Though it is giving correct link. But I need link for only URL column and real values for rest two columns?

 

Please could you suggest..

 

Thanks & regards,

Aman

Link to comment
Share on other sites

Dear Guru's,

I have downlaoded other way of doing o/p for same query through net:

 

<html>

<body>

<?php

$username="username";

$password="xxxxx";

$database="databse";

 

mysql_connect(localhost,$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT Description, URL FROM uploads";

$result=mysql_query($query);

 

$num=mysql_num_rows($result);

 

mysql_close();

?>

<table border="0" cellspacing="2" cellpadding="2">

<tr>

<th><font face="Arial, Helvetica, sans-serif">Description</font></th>

<th><font face="Arial, Helvetica, sans-serif">Url</font></th>

</tr>

 

<?php

$i=0;

while ($i < $num) {

 

$f1=mysql_result($result,$i,"Description");

$f2=mysql_result($result,$i,"URL");

?>

 

<tr>

<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>

<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2;  ?></font></td>

 

</tr>

 

<?php

$i++;

}

?>

</body>

</html>

 

 

Is it easier to change something in this to get URl has Hyperlink and value displayed as link?

 

Thanks & regards,

Aman

Link to comment
Share on other sites

thats because you have a for loop that does the same thing for every column regardless or which column it is.

 

//assuming we just want to echo the id, resource ID and then a link, we can do
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['resourceID'] . "</td>";
echo "<td><a href='" . $row['URL'] . "'>Link or Script</a></td>";

 

obviously you will need to change the keys I used to the correct column names you are grabbing with your select statement. This would replace your for loop.

 

EDIT: I wouldnt use mysql_result, since its easier and faster to just grab the whoel row in 1 go. If you wanted to replace the link text (in this case, Link or Script) with the value (i'm not sure what value you are referring to) then whatever column has the value, use that column as a key in the $row array.

 

Hope this helps

Link to comment
Share on other sites

Dear Guru,

Thanks for your guidance. I think we are getting very close on this now:

 

As suggested by you, I have changed earlier code to below code:

 

------------------------------------------

<?php

$db_host = 'localhost';

$db_user = 'user';

$db_pwd = 'xxxx';

 

$database = 'website';

$table = 'uploads';

 

if (!mysql_connect($db_host, $db_user, $db_pwd))

    die("Can't connect to database");

 

if (!mysql_select_db($database))

    die("Can't select database");

 

// sending query

$result = mysql_query("select Description, URL from uploads");

if (!$result) {

    die("Query to show fields from table failed");

}

 

$fields_num = mysql_num_fields($result);

 

echo "<h1>Table: {$table}</h1>";

echo "<table border='1'><tr>";

// printing table headers

for($i=0; $i<$fields_num; $i++)

{

    $field = mysql_fetch_field($result);

    echo "<td>{$field->name}</td>";

}

echo "</tr>\n";

// printing table rows

while($row = mysql_fetch_row($result))

{

    echo "<tr>";

  echo "<td>" . $row['Description'] . "</td>";

  echo "<td><a href='" . $row['URL'] . "'>Link or Script</a></td>";

 

    echo "</tr>\n";

}

mysql_free_result($result);

?>

</body></html>

"tabletest22.php" 47L, 968C written                                                                                                                           

[amansood@gator126 ~/public_html]$ vi tabletest22.php

 

<?php

$db_host = 'localhost';

$db_user = 'amansood_1';

$db_pwd = 'simple1u';

 

$database = 'amansood_site';

$table = 'uploads';

 

if (!mysql_connect($db_host, $db_user, $db_pwd))

    die("Can't connect to database");

 

if (!mysql_select_db($database))

    die("Can't select database");

 

// sending query

$result = mysql_query("select Description, URL from uploads");

if (!$result) {

    die("Query to show fields from table failed");

}

 

$fields_num = mysql_num_fields($result);

 

echo "<h1>Table: {$table}</h1>";

echo "<table border='1'><tr>";

// printing table headers

for($i=0; $i<$fields_num; $i++)

{

    $field = mysql_fetch_field($result);

    echo "<td>{$field->name}</td>";

}

echo "</tr>\n";

// printing table rows

while($row = mysql_fetch_row($result))

{

    echo "<tr>";

 

echo "<td>" . $row['Description'] . "</td>";

echo "<td><a href='" . $row['URL'] . "'>Link or Script</a></td>";

 

    echo "</tr>\n";

}

mysql_free_result($result);

?>

</body></html>

 

-------------------------------------------------------

 

The o/p to this code is as follows:

 

Table: uploads

Description URL

Link or Script

 

Description is NULL ( Don't know why is that :-( )  Also, the "Link or Script" Hyperlink points to Current page, I want it to point to actual value of column URL

which is "http://servername.domain.com/directly/Script.sql"

 

I need this Link on URL column ( with above value) to enable users click it and download this particular script which currently resides in server file system. Only, Complete URl is mentioned in URL column.

 

Apologise for confusing you earlier. Hope I am bit clear this time otherwise feel free to ask, I can give call and expalin as well :-(

 

Thanks & regards,

Aman

 

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.