2012-08-29 26 views
0

我創建了一個連接數據庫的php文件,從表中獲取數據並以json格式顯示它。url上的PHP/JSON參數

該文件在被調用的index.php。

要查看JSON我剛去的文件瀏覽器:

http://127.0.0.1/json/index.php and it displays: 

{"title":[{"id":"1","title":"Title1","desc":"Description1"},{"id":"2","title":"Title2","desc":"Description2"}]} 

我需要做的是能夠通過添加參數,如過濾此:

For example: http://127.0.0.1/json/index.php?id=1 to just show the data with an id of 1 but it still shows all the data. 

這裏是php代碼:

<?php 

$username = "root"; 
$password = ""; 
$hostname = "localhost"; 

//connection to the database 
$dbhandle = mysql_connect($hostname, $username, $password) 
    or die("Unable to connect to MySQL"); 

$selected = mysql_select_db("mydb",$dbhandle) 
    or die("Could not select mydb"); 

$result = mysql_query("SELECT * FROM contacts"); 
$rows = array(); 
    while($r = mysql_fetch_assoc($result)) { 
    $rows['title'][] = $r; 
    } 

print json_encode($rows); 

?> 

我在做什麼錯在這裏或失蹤?

+0

請不要用'mysql_ *對新代碼'功能。他們不再被維護,社區已經開始[棄用流程](http://news.php.net/php.internals/53799)。看到[**紅框**](http://php.net/mysql-connect)?相反,您應該瞭解[準備好的陳述](http://en.wikipedia.org/wiki/Prepared_statement)並使用[PDO](http://php.net/pdo)或[MySQLi](http:// php.net/mysqli)。如果你不能決定,試試[這篇文章](http://php.net/mysqlinfo.api.choosing)。如果你關心學習,[這裏是很好的PDO教程](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers)。 –

回答

1
<?php 
    $username = "root"; 
    $password = ""; 
    $hostname = "localhost"; 

    //connection to the database 
    $dbhandle = mysql_connect($hostname, $username, $password) 
    or die("Unable to connect to MySQL"); 

    $selected = mysql_select_db("mydb",$dbhandle) 
    or die("Could not select mydb"); 

    $id = 0; 
    if(isset($_GET['id'])){ $id = (int)$_GET['id']; } 

    if(!$id){ 
     $query = "SELECT * FROM `contacts`"; 
    } else { 
     $query = "SELECT * FROM `contacts` WHERE `id`='".$id."'"; 
    } 
    $result = mysql_query($query); 
    $rows = array(); 
    while($r = mysql_fetch_assoc($result)) { 
     $rows['title'][] = $r; 
    } 
    print json_encode($rows); 
?> 
+0

我是否在寫入結果中添加此代碼...? – Satch3000

+0

是的,從$ result一路下來 –

+0

獲取錯誤:SCREAM:錯誤抑制忽略...注意:未定義變量:導致C:\ wamp \ www \ json \ index.php在第23行..這是第23行:而($ R = mysql_fetch_assoc($結果)){ – Satch3000

0

您需要將where條件添加到您的查詢。

$id = (int) $_GET['id']; 
$result = mysql_query("SELECT * FROM contacts WHERE id = $id"); 
1

更改以下

$result = mysql_query("SELECT * FROM contacts"); 

$id = $_REQUEST['id']; 

$query = 'SELECT * FROM contacts'; 

if(is_numeric($id)) 
    $query .= ' WHERE id = ' . $id; 

$result = mysql_query($query); 
+0

這幾乎是完美的,但如果我不進行過濾,我會得到一個錯誤。而我需要的過濾是可選 – Satch3000

1

對於一個你必須添加WHERE到您的SQL語句....

SELECT * FROM `contacts` WHERE `id` = $id 

你在哪裏看到id這應該是表中id列的名稱,不管它是什麼。但你也必須首先消毒輸入...

if(!is_numeric($_GET['id'])) 
    exit; // if not a number then exit 

$id = mysql_real_escape_string($_GET['id']); // escape the input 

當然,這是最基本的錯誤檢查。你可以解釋它。所以,你的代碼看起來會像這樣...

<?php 

$username = "root"; 
$password = ""; 
$hostname = "localhost"; 

//connection to the database 
$dbhandle = mysql_connect($hostname, $username, $password) 
    or die("Unable to connect to MySQL"); 

$selected = mysql_select_db("mydb",$dbhandle) 
    or die("Could not select mydb"); 

if(!is_numeric($_GET['id']) || !$_GET['id']) 
    exit; // if not an integer or id not set then exit 

$id = mysql_real_escape_string($_GET['id']); // escape the input 

$result = mysql_query("SELECT * FROM contacts WHERE id = $id"); 
$rows = array(); 
    while($r = mysql_fetch_assoc($result)) { 
    $rows['title'][] = $r; 
    } 

print json_encode($rows); 

?> 

你真的不應該使用根連接到數據庫在Web應用程序。 Mihai也是對的,您應該使用PDO,但對於這樣一個簡單的應用程序來說並不是必須的。

編輯 但是,上面的代碼將需要一個id輸入。如果你想仍然能夠得到整個列表,如果沒有id提供它看起來像這樣...

<?php 

$username = "root"; 
$password = ""; 
$hostname = "localhost"; 

//connection to the database 
$dbhandle = mysql_connect($hostname, $username, $password) 
    or die("Unable to connect to MySQL"); 

$selected = mysql_select_db("mydb",$dbhandle) 
    or die("Could not select mydb"); 

$sql = "SELECT * FROM `contacts`"; 

if(isset($_GET['id']) && $_GET['id'] > 0) { 
    // if id is set then add the WHERE statement 
    if(!is_numeric($_GET['id'])) 
     die('id must be an integer'); // if id is not an integer then exit 
    $id = mysql_real_escape_string((int)$_GET['id']); // escape the input 
    $sql .= " WHERE `id` = $id"; // append the WHERE statement to the sql 
} 


$result = mysql_query($sql); 
$rows = array(); 
    while($r = mysql_fetch_assoc($result)) { 
    $rows['title'][] = $r; 
    } 

print json_encode($rows); 

?> 
+0

如果我硬編碼在SQL查詢的ID我被迫要把它過濾。我需要它是可選的,所以我可以顯示全部或通過url添加過濾器。 – Satch3000

+0

我編輯了上面的帖子,將其考慮在內。 – DerekIsBusy

+0

四處錯誤:SCREAM:錯誤抑制忽略 通知(!):未定義指數:ID在C:?\ WAMP \ WWW \ JSON \的index.php上線16,當我添加的ID = 1的頁面是空白 – Satch3000