Forum Moderators: coopster

Message Too Old, No Replies

making a link of a mysql result

         

raulwg

1:03 pm on Apr 28, 2003 (gmt 0)

10+ Year Member



i want to make a link out of the results of a mysql query but it doesn't work.....

this is the script:

echo "<a href=\"http://"$myrow["link"]">"$myrow["place"]"</a>";

Greetz, Raśl

grahamstewart

1:32 pm on Apr 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try..

echo '<a href="http://'.$myrow['link'].'>'.$myrow['place'].'</a>';

Birdman

1:38 pm on Apr 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or this should work as well:


echo "<a href='http://$myrow[link]'>$myrow[place]</a>";

I usually use single quotes around my attributes when echoing or printing so I don't have to escape them.

I also don't use quotes around the MySQL field(i.e. [link])

Don't rightly know if my methods are correct, but it seems to work for me.

bonanza

1:44 pm on Apr 28, 2003 (gmt 0)



When I'm going to be doing a lot in my script with database results, I save them to regular variables first:

$this_link = $myrow["link"];
$this_place = $myrow["place"];
echo "<a href=\"http://$this_link]\">$this_place</a>";

Makes it a little easier to create and read the html.

If you use double-quotes, you can reference variables directly. If you use single quotes, you won't need to escape the double quotes, but PHP won't translate the variables. Whatever you prefer.

raulwg

1:55 pm on Apr 28, 2003 (gmt 0)

10+ Year Member



thanx for all the answers, tips and making my first post a success....

willybfriendly

3:40 pm on Apr 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When I'm going to be doing a lot in my script with database results, I save them to regular variables first:

$this_link = $myrow["link"];
$this_place = $myrow["place"];
echo "<a href=\"http://$this_link]\">$this_place</a>";

Why not just use extract($myrow);?

I agree, this makes code much more readable.

WBF

andreasfriedrich

4:52 pm on Apr 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or this should work as well:


echo <<<END_OF_HTML
<a href="http://$myrow[link]">$myrow[place]</a>
END_OF_HTML;

Andreas

Birdman

4:54 pm on Apr 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What are the benefits of using the heredoc(I think that's what it's called) method, Andreas?

bonanza

5:08 pm on Apr 28, 2003 (gmt 0)



Why not just use extract($myrow);?

Absolutely! I figured that would complicate my post.

What are the benefits of using the heredoc

Simplicity and code readability especially for big blocks of HTML riddled with PHP. (And it feels like Perl :))

Birdman

5:12 pm on Apr 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks bonanza,

I will start using that method, just wanted to know why I should.

andreasfriedrich

6:05 pm on Apr 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is a post on Using heredoc syntax [webmasterworld.com] already in the Bag-O-Tricks for PHP II [webmasterworld.com].

Another benefit of using heredoc syntax is that it save you from having to escape any kind of quotes within your string.

Andreas

grahamstewart

11:18 pm on Apr 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I also don't use quotes around the MySQL field(i.e. [link])

I'm pretty sure that this will cause an 'unknown define' warning to be generated. However, php just assumes that link is defined as 'link' so your code still works - but I wouldn't recommend it.

If your not seeing a warning yourself then you probably don't have the error level turned up - have a look in php.ini and put it all the way up to error_reporting = E_ALL for development work.

echo <<<END_OF_HTML

I like the Heredoc syntax too, but unfortunately it causes the syntax highlighting in Textpad to get completely confused. Anyone got a php syntax definition file for textpad that can handle it?Or know of a better editor to write php in?

andreasfriedrich

12:22 am on Apr 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you want to interpolate a certain array element into a double quoted or here doc delimited string then - using the simple syntax - one must not use $myrow['fieldname']. Only $myrow[fieldname] will work. This is an exception to the general rule that you need to quote hash keys.

Andreas

grahamstewart

12:48 am on Apr 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My bad: didn't know that - guess I should have tested before shooting my mouth off :)