Replacing text with a url using JQUERY + PHP

Replacing text with a url using JQUERY + PHP

Hi
i have a database (dictionary) with 2 variables (txtfield, txturl)
each txtfield- is a word
each txtfield- is url
i want to create a script that replaces the matching words with urls using jquery function created with php.

My code so far is this:
---------------code----------------------
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
<?php
// This connects to database and retreives txtfield and txturl
$db="mydatabase";
$link = mysql_connect("localhost");
mysql_select_db($db , $link)
$result = mysql_query( "SELECT txtfield, txturl FROM dictionary" )

//starts a loop creating a javascript string that replaces the words in body with url
while ($row = mysql_fetch_assoc($result)) {
$txtfield = $row['txtfield'];
$txturl = $row['txturl'];
echo "var".$txtfield." = $('".$txtfield."');";
echo $txtfield.".val(".$txtfield.".val().replace(".$txtfield.",'<a href='http://".$txturl."/'>This is my url from txturl </a>') );";

}
?>
//end of php loop
});
</script>
<body>
Some text with a matching word in variable txtfield of the database dictionary
</body>
------------------

Can anyone help me to find the way to find and replace words with this framework?

Thanks
M.