amansood4u Posted June 26, 2011 Share Posted June 26, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/240445-need-help-to-display-url-stored-in-mysql-as-hyperlink-in-php/ Share on other sites More sharing options...
wildteen88 Posted June 26, 2011 Share Posted June 26, 2011 Post your current code here. Quote Link to comment https://forums.phpfreaks.com/topic/240445-need-help-to-display-url-stored-in-mysql-as-hyperlink-in-php/#findComment-1235006 Share on other sites More sharing options...
amansood4u Posted June 26, 2011 Author Share Posted June 26, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/240445-need-help-to-display-url-stored-in-mysql-as-hyperlink-in-php/#findComment-1235011 Share on other sites More sharing options...
amansood4u Posted June 26, 2011 Author Share Posted June 26, 2011 Just to make things more clear the URL column has value like http://servername.domain.com/folder/filename.sql So, it's complete nothing to add or substract. If, cutting it short is the option for href to work i can easily do that as well. Thanks & regards, Aman Quote Link to comment https://forums.phpfreaks.com/topic/240445-need-help-to-display-url-stored-in-mysql-as-hyperlink-in-php/#findComment-1235015 Share on other sites More sharing options...
EdwinPaul Posted June 26, 2011 Share Posted June 26, 2011 Just change echo "<td>$cell</td>"; to echo "<td><a href='.$cell.'>Link or Script</a></td>"; and it should work.. Quote Link to comment https://forums.phpfreaks.com/topic/240445-need-help-to-display-url-stored-in-mysql-as-hyperlink-in-php/#findComment-1235017 Share on other sites More sharing options...
PFMaBiSmAd Posted June 26, 2011 Share Posted June 26, 2011 echo "<td><a href='.$cell.'>Link or Script</a></td>"; ^^^ You would want to remove the dots from that since you are not concatenating a variable and the current result would produce an invalid href with leading and trailing dots. Quote Link to comment https://forums.phpfreaks.com/topic/240445-need-help-to-display-url-stored-in-mysql-as-hyperlink-in-php/#findComment-1235020 Share on other sites More sharing options...
EdwinPaul Posted June 26, 2011 Share Posted June 26, 2011 Right, thanks for the correction. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/240445-need-help-to-display-url-stored-in-mysql-as-hyperlink-in-php/#findComment-1235022 Share on other sites More sharing options...
amansood4u Posted June 26, 2011 Author Share Posted June 26, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/240445-need-help-to-display-url-stored-in-mysql-as-hyperlink-in-php/#findComment-1235027 Share on other sites More sharing options...
amansood4u Posted June 26, 2011 Author Share Posted June 26, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/240445-need-help-to-display-url-stored-in-mysql-as-hyperlink-in-php/#findComment-1235031 Share on other sites More sharing options...
mikesta707 Posted June 26, 2011 Share Posted June 26, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/240445-need-help-to-display-url-stored-in-mysql-as-hyperlink-in-php/#findComment-1235036 Share on other sites More sharing options...
amansood4u Posted June 26, 2011 Author Share Posted June 26, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/240445-need-help-to-display-url-stored-in-mysql-as-hyperlink-in-php/#findComment-1235048 Share on other sites More sharing options...
amansood4u Posted June 26, 2011 Author Share Posted June 26, 2011 Dear Guru's, If somebody can guide me on how to make a variable in php code as hyperlink, that should solve my issue. I have $e > which is o/p value(cell) of 1 column and I want to display it as hyperlink. Thanks & regards, Aman Quote Link to comment https://forums.phpfreaks.com/topic/240445-need-help-to-display-url-stored-in-mysql-as-hyperlink-in-php/#findComment-1235075 Share on other sites More sharing options...
EdwinPaul Posted June 26, 2011 Share Posted June 26, 2011 In stead of: echo "<td><a href='" . $row['URL'] . "'>Link or Script</a></td>"; use: echo "<td><a href='" . $row['URL'] . "'>'" . $row['URL'] . "</a></td>"; and you will see your link which is also a hyperlink Quote Link to comment https://forums.phpfreaks.com/topic/240445-need-help-to-display-url-stored-in-mysql-as-hyperlink-in-php/#findComment-1235119 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.