【问题标题】:SQL query in php file errorphp文件中的SQL查询错误
【发布时间】:2013-07-15 23:05:34
【问题描述】:

我有这段代码从一个 url 传递一个变量。当我使用 $_GET 方法时,它会返回一个没有找到产品的 json,但是当我手动给出 $user_email 从 url 获得的值时,它会返回正确的 json!出了什么问题,我该如何纠正?谢谢

网址: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);
        }
?>

【问题讨论】:

  • var_dump($_GET['user_email']) 输出什么?
  • 而且您也不会发布到此方法?
  • string(1) "m" 这是 var_dump($_GET['user_email']) 的输出
  • 如果不是 $user_email= $_GET['user_email'];我有 $user_email='m';它工作正常!我真的无法理解问题出在哪里!请帮忙

标签: php android sql json


【解决方案1】:

试试这个:

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

编辑:

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

【讨论】:

  • 它使用我查询中的值。请解释一下。
  • 抱歉,您的第一个 正确的(PHP 变量扩展在 " 双引号字符串中)。第二个,没有 SQL 引号作为结果字符串文字,不是。两者都不是然而,这些解决了 OP 代码中的一个问题——他们的代码行是正确且格式正确的。
  • 我并不是直言不讳,但除非您最终运行他的代码,否则这不是您的决定。
  • 不引用字符串文字的 SQL 查询是不正确的语法。他的原始代码行显然是正确且格式正确的。如果您有其他参考,请发布。你在这里给人的印象是效率低下和不礼貌。我建议您参考 ANSI SQL 标准和 MySQL 参考以进行进一步的教育:dev.mysql.com/doc/refman/5.0/en/string-literals.html
  • forums.phpfreaks.com/topic/… 我并不想表现得不礼貌或没有效率。也许您的时间可以更好地用于解决海报问题,而不是降低试图提出建议的人的等级。我个人在 mysql 查询中使用不同的字符串注释得到了不同的结果。因此,让所有人都在这里找到解决方案。
猜你喜欢
  • 2012-10-06
  • 2016-09-29
  • 2015-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多