从PHP到Javascript的变量

I have this code in html:

<a id="<?echo $result[id]?>" onclick="displayResult()"></a> 

And i want to get this id number from link without refresh the page to this function:

<script>
function displayResult()
{ 
<?
connection = mysql_connect('localhost', 'root', '...');
$db = mysql_select_db('webfaturas', $connection);
$sql = mysql_query("SELECT * FROM `artigos` WHERE id = $id");
$resultado = mysql_fetch_array($sql); 
?> 
</script>

So i can do sql query WHERE id = id that cames from without refresh the page.

What you'll want to do is grab the ID of the link via javascript, and POST it to your php script using AJAX. jQuery makes this easier than using just straight javascript - I suggest reading the documentation. http://api.jquery.com/jQuery.post/

Like the comments have said using ajax to get what you need is the best way to go about doing it.

Wikipdia says: http://en.wikipedia.org/wiki/Ajax_(programming) tl;dr

Basically you make a call to another page using javascript and any data you collected. It will then return some sort of data structure (usually JSON or XML)

Then render the results.

Thats the simple way to do it.

(I know the internets will hate me for this) jQuery has a function just for it called ajax

Like other people have said, AJAX is the best way to go about this.

jQuery provides the easiest way to do this.

Send the ID to a PHP page using jQuery's $.ajax function, get the data using either $_GET["id"] or $_POST["id"], depending on how you sent the AJAX, do your SQL query and then send a response back to the browser (as if it were a normal web page, using echo).