Jump to content

Building a url link in php from MySQL


kahodges

Recommended Posts

I'm trying to pull a url from a database table and display it as a html hyperlink in php. I'm getting the link to display, but I'm having trouble getting it to pull the MySQL data into the field. Thanks in advance if you can steer me in the right direction. Here is my code:

 

<html>
<body>
<?php
$username="username";
$password="password";
$database="database_name";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM `jobs` WHERE ( `url` is not NULL and trim(`url`) <> '' )";
$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_close();
?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Job #</font></th>
<th><font face="Arial, Helvetica, sans-serif">Company</font></th>
<th><font face="Arial, Helvetica, sans-serif">Location</font></th>
<th><font face="Arial, Helvetica, sans-serif">Map</font></th>
</tr>

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"id");
$f2=mysql_result($result,$i,"job");
$f3=mysql_result($result,$i,"company");
$f4=mysql_result($result,$i,"location");
$f5=mysql_result($result,$i,"url");
$site="https://www.mysite.com/maps/";
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo( '<a href="$site.$f5">$f5</a>'); ?></font></td>
</tr>

<?php
$i++;
}
?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/206538-building-a-url-link-in-php-from-mysql/
Share on other sites

I'm trying to get it to display as so:

 

https://www.mysite.com/maps/file_name.pdf

 

Instead, I'm getting:

 

$site.$f5 displaying as the url.

 

I went through as did the following change:

Removed the line containing: $site="https://www.mysite.com/maps/";

and changed the line below:


<?php echo( '<a href="https://www.mysite.com/maps/.$f5">Map</a>'); ?>

 

Now, I'm getting it to display as:

https://www.mysite.com/maps/.$f5 as the url. I need $f5 to display the file name out of MySQL.

 

Thanks.

 

I read it, but I'm not understanding. Are you saying there should be single quotes instead of the double quotes around the url in this line?:

 

<?php echo( '<a href="https://www.mysite.com/maps/.$f5">Map</a>'); ?>

 

If that is what you mean, I've tried it, and I get a Parse error: syntax error, unexpected T_STRING. If you can point out where I'm messing up, and be a little clearer on what you mean, I'd appreciate it.

 

Thanks.

Strings within single quotes are not evaluated, those within double quotes are, Try this script to see the difference:

<?php
$str = 'This is a test';
echo $str . '<br>';
echo '$str<br>';
echo "$str<br>";
?>

 

Ken

 

 

I'm getting the following error on that script:

Parse error: syntax error, unexpected T_VARIABLE

 

I do appreciate your trying to help me understand. Thanks.

I figured out what was I was doing wrong on where I tried your code, and I see the difference now, and think I understand. I have the code as:

<td><font face="Arial, Helvetica, sans-serif">
<?php echo ("<a href=https://www.teamcrr.com/maps$f5>Map</a>)"?></font></td>

That code shows as valid in my editor.

Now, my problem is that it's throwing the following error:

Parse error: syntax error, unexpected ';' in /home/directory/public_html/map.php on line 39

 

Line 39 is the code I have displayed above, and I know I'm getting old and half blind, but I don't see where there's a ; anywhere in that line. Anyone know what I need to do?

 

Here's the entire script:

<html>
<body>
<?php
$username="username";
$password="password";
$database="database_name";

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM `jobs` WHERE ( `url` is not NULL and trim(`url`) <> '' )";
$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_close();
?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">job #</font></th>
<th><font face="Arial, Helvetica, sans-serif">company</font></th>
<th><font face="Arial, Helvetica, sans-serif">location</font></th>
<th><font face="Arial, Helvetica, sans-serif">map</font></th>
</tr>

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"id");
$f2=mysql_result($result,$i,"job");
$f3=mysql_result($result,$i,"company");
$f4=mysql_result($result,$i,"location");
$f5=mysql_result($result,$i,"url");
?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo ("<a href=https://www.mysite.com/maps/$f5>Map</a>)"?></font></td>
</tr>

<?php
$i++;
}
?>
</body>
</html>

 

This is line 39:

<td><font face="Arial, Helvetica, sans-serif"><?php echo ("<a href=https://www.teamcrr.com/maps/$f5>Map</a>)"?></font></td>

Use this:

<td><font face="Arial, Helvetica, sans-serif"><?php echo ("<a href=https://www.teamcrr.com/maps/$f5>Map</a>");?></font></td>

 

When you use the full <?php tag to echo a variable, the echo needs to end with a ;.  Also, your closing parenthesis was the wrong side of the closing double quote.

Archived

This topic is now archived and is closed to further replies.

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