I am new to PHP. I have a MySQL database with a column of URL's. I have a PHP script that randomizes the URL's and echoes the result to the browser. I just need a need a way to turn the randomized URL into a clickable link. I would imagine maybe I need to use javascript to do this but I am not sure. I have done a ton of research but cannot find a solution, but maybe I am not looking for the correct solution. If I need to post my script just let me know. Thanks.

<?php
$result = mysql_query("SELECT url, title FROM links_table ORDER BY RAND() LIMIT 1");

$row = mysql_fetch_row($result);

$url = $row[0];
$title = $row[1];
echo "<a href=\"$url\">$title</a>";
?>
<?php
$result = mysql_query("SELECT url, title FROM links_table ORDER BY RAND() LIMIT 1");

$row = mysql_fetch_row($result);

$url = $row[0];
$title = $row[1];
echo "<a href=\"$url\">$title</a>";
?>

Thanks for the quick response cscgal. The problem with your solution is that I already have links setup on the page, so I am not able to echo them. Therefore I need a way to assign the query or variable to the existing links on the page. Here is the PHP script I already have:

<?php
//Open random table record
$range_result = mysql_query( " SELECT MAX(`id`) AS max_id , MIN(`id`) AS min_id FROM `table` ");
$range_row = mysql_fetch_object( $range_result );
$random = mt_rand( $range_row->min_id , $range_row->max_id );
$result = mysql_query( " SELECT * FROM `table` WHERE `id` >= $random LIMIT 0,1 ");

//Display record
while ($row = mysql_fetch_assoc($result)) {
    echo $row["url"];
}
?>

It echoes the url properly as plain text, I am just hoping there is a better way besides echoing it so I can assign the URL to my links. But I suppose with php that is the only way to fetch the data without some client side code?

Either echo the <a> Tag around the url or just use cscgal's version. It is basically and more efficient version of what you are doing and it can replace what you have done.

or you just need to change

//Display recordwhile ($row = mysql_fetch_assoc($result)) {    echo $row["url"];}

to

//Display recordwhile ($row = mysql_fetch_assoc($result)) {    echo "<a href="".$row["url"] ."">".$row["url"] ."</a>";}

Thank you both. I was able to get it working by using a combination of what you both suggested:

<?
//Display record
while ($row = mysql_fetch_assoc($result)) {    
echo "<a href=\"".$row['url']."\" target=\"_new\">Link</a>";
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.