2014-12-03 62 views
1

我試圖從數據庫是URL 「http://www.google.com/PHP的GET字符串包含URL從數據庫

但數據我得到改變這個HTTP GET字符串:\/\ /www.google.com \/

while($row = mysql_fetch_array($result)){ 
    // temporary array to create single category 
    $tmp = array(); 
    $tmp["id"] = $row["id"]; 
    $tmp["name"] = $row["name"]; 
    $tmp["url"]= $row["url"]; 

    array_push($response["database"], $tmp); 
} 

我怎樣才能得到的網址沒有改變。

+2

[**在新的代碼,請不要使用'mysql_ *'功能**](http://bit.ly/phpmsql生成的文件同治創建另一個文件, )。他們不再被維護[並且被正式棄用](http://j.mp/XqV7Lp)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 – esqew 2014-12-03 05:56:27

+0

問題仍然存在,我嘗試使用PDO和MySQLi都不工作..實際上我在json中工作,我把這個數組放在json_encode()中,你認爲這個問題是由數組引起的嗎? – 2014-12-03 19:58:15

回答

2

在你的例子中,兩段數據是相同的,StackOverflow是否重新格式化你的數據?你的代碼對我來說看起來很好,也許這是數據如何被插入到數據庫而不是如何被檢索的問題。你有沒有看過像phpMyAdmin或SQLyog Community Edition這樣的SQL瀏覽器中的數據,以確認數據是否按預期存儲?

+0

問題仍然存在,我嘗試使用PDO和MySQLi都無法正常工作..實際上我在json中工作,並且我把這個數組放在了json_encode()中,你認爲這個問題是由數組引起的嗎? – 2014-12-03 19:55:33

1

我解決這個問題其實 我想創建以.json文件,但是當我們使用數組和顯示數據,如URL在PHP頁面.PHP中的數據會改變,因爲它解讀爲一個HTML代碼,在JSON的情況下,我們有在從數據庫中獲取數據後

$response = array(); $response["feed"] = array(); 

foreach($db->query('SELECT * FROM table') as $row) { 

    $tmp = array(); 
    $tmp['id'] = $row['id']; 
    $tmp['name'] = $row['name']; 
    $tmp['url']= $row['url']; 
    array_push($response['table'], $tmp);   } 

//here the data posted into php page so the url change 
echo json_encode($response); 
//here create .json data and write data in data.json 
$fp = fopen('data.json', 'w'); 
fwrite($fp, json_encode($response)); 
fclose($fp);