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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

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.