2013-07-15 65 views
0

我有這段代碼從一個url傳遞一個變量。當我使用$ _GET方法時,它會返回一個沒有找到產品的json,但是當我手動給出$ user_email從url返回的值時,它會返回正確的json!什麼是錯的,我該如何糾正它?謝謝php文件錯誤中的SQL查詢

網址:HTTP:// * ** * ** * ** * * /android_connect/get_all_products.php?user_email=m

<?php 

/* 
* Following code will list all the products 
*/ 

// array for JSON response 
$response = array(); 

$user_email= $_GET['user_email']; 



// include db connect class 
require_once __DIR__ . '/db_connect.php'; 

// connecting to db 
$db = new DB_CONNECT(); 

// get all products from products table 
$test= "SELECT *FROM products WHERE user_email= '" .$user_email. "'"; 


//echo $test; 
$result = mysql_query($test) or die(mysql_error()); 








// check for empty result 
if (mysql_num_rows($result) > 0) { 
    // looping through all results 
    // products node 
    $response["products"] = array(); 

    while ($row = mysql_fetch_array($result)) { 
     // temp user array 
     $product = array(); 
     $product["pid"] = $row["pid"]; 
     $product["firstname"] = $row["firstname"]; 
     $product["lastname"] = $row["lastname"]; 
     $product["email"] = $row["email"]; 
     $product["phone"] = $row["phone"]; 
     $product["address"] = $row["address"]; 
     $product["created_at"] = $row["created_at"]; 
     $product["updated_at"] = $row["updated_at"]; 
     $product["user_email"] = $row["user_email"]; 


     // push single product into final response array 
     array_push($response["products"], $product); 
    } 
    // success 
    $response["success"] = 1; 

    // echoing JSON response 
    echo json_encode($response); 
    } else { 
    // no products found 
$response["success"] = 0; 
$response["message"] = "No products found"; 

    // echo no users JSON 
    echo json_encode($response); 
     } 
?> 
+0

var_dump($ _ GET ['user_email'])'output'是什麼? – ponysmith

+0

並且您沒有任何機會發布到此方法? – nickL

+0

string(1)「m」這是var_dump的輸出($ _GET ['user_email']) – appLogic

回答

-1

試試這個:

$test= "SELECT * FROM products WHERE user_email = '$user_email'"; 

編輯:

$test= "SELECT * FROM products WHERE user_email = $user_email"; 
+0

它使用我的查詢中的值。請解釋。 –

+0

對不起,你的第一個*是*正確的(在雙引號字符串中的PHP變量擴展)第二個沒有SQL引用的結果字符串不是。然而,這些都不能解決OP代碼中的問題 - - 他們的代碼行是正確的&well-formed。 –

+0

我並不是說要生硬,但除非你在你的末端運行他的代碼,否則這不是你的決定。 –