2011-05-26 23 views
0

我試圖讓一個PHP頁面,我可以導航到喜歡... http://mydomain.com?id=12345如何通過查詢同一行中的另一個值來返回特定mysql單元格中的whats?

在我mysql表

有一個ID列和一個文本列....我怎樣才能使我的PHP頁面得到該ID,找到它,然後返回在同一行中的文本單元格中的什麼,並在頁面上回顯它?

這就是我到目前爲止..我主要是堅持我應該做的與Mysql查詢。那麼如何將數據實際獲取到可以回顯給頁面的變量中。謝謝!

編輯:你已經構建了您的查詢後,取得了什麼進展?

<?php 

    mysql_connect("my.mysql.com", "user", "pass"); 
    mysql_select_db("mydb"); 

    $id= $_GET['id']; 

    $result = mysql_query("SELECT text FROM mytable WHERE id='$id'") 
or die(mysql_error()); 


echo nl2br($result); 


    ?> 

回答

2

右鍵解壓,將它傳遞給數據庫並獲取結果


// Perform Query 
$result = mysql_query($query); 

// Check result 
// This shows the actual query sent to MySQL, and the error. Useful for debugging. 
if (!$result) { 
    $message = 'Invalid query: ' . mysql_error() . "\n"; 
    $message .= 'Whole query: ' . $query; 
    die($message); 
} 

// Use result 
// Attempting to print $result won't allow access to information in the resource 
// One of the mysql result functions must be used 
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc. 
while ($row = mysql_fetch_assoc($result)) { 
    echo $row['field1']; 
    echo $row['field2']; 
} 

// Free the resources associated with the result set 
// This is done automatically at the end of the script 
mysql_free_result($result);

重要注意事項:在將數據輸入到數據庫之前,應始終對其數據進行消毒處理,以避免sql注入

例如:想象某人將「'; 。刪除表MYTABLE;」爲在URL中的ID,然後在通過這個到MySQL會刪除表

注2:輸出你的帖子的時候,一定要逃避某些字符:不是<>。你應該把& LT& GT

Recommended tutorial

here複製的腳本

2

指定領域SELECT

SELECT field1, field2 
    FROM mytable 
    WHERE id=:id 
相關問題