kahodges Posted July 2, 2010 Share Posted July 2, 2010 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> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted July 2, 2010 Share Posted July 2, 2010 What kind of trouble, specifically, are you having with this? Quote Link to comment Share on other sites More sharing options...
kahodges Posted July 2, 2010 Author Share Posted July 2, 2010 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. Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted July 2, 2010 Share Posted July 2, 2010 http://us2.php.net/manual/en/language.types.string.php Read the single and double quote sections. Quote Link to comment Share on other sites More sharing options...
kahodges Posted July 2, 2010 Author Share Posted July 2, 2010 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. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 2, 2010 Share Posted July 2, 2010 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 Quote Link to comment Share on other sites More sharing options...
kahodges Posted July 2, 2010 Author Share Posted July 2, 2010 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. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 2, 2010 Share Posted July 2, 2010 If you're getting that error message, you didn't enter the script correctly. There are no syntax errors in those lines. What line does the error point to? Ken Quote Link to comment Share on other sites More sharing options...
kahodges Posted July 2, 2010 Author Share Posted July 2, 2010 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? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 2, 2010 Share Posted July 2, 2010 Is there any PHP code before that line? Ken Quote Link to comment Share on other sites More sharing options...
kahodges Posted July 2, 2010 Author Share Posted July 2, 2010 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> Quote Link to comment Share on other sites More sharing options...
Cagecrawler Posted July 2, 2010 Share Posted July 2, 2010 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. Quote Link to comment Share on other sites More sharing options...
kahodges Posted July 2, 2010 Author Share Posted July 2, 2010 I took the brackets out, and it's working fine now: <td><font face="Arial, Helvetica, sans-serif"><?php echo "<a href=https://www.mysite.com/maps/$f5>Map</a>"?></font></td> Thanks for everyone's help. Quote Link to comment 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.