Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

select statement always return the last inserted row in php mysql

When I wrote the select statement it always return the last inserted row in the database. What is the problem, and how can I fix it?

Important NOTE: A friend of mine took the same code and it worked for her properly!

    if (isset($_GET["name"])) {
    $pid = $_GET['name'];

    // get a product from products table
    //)or die(mysql_error()
    $result = mysql_query("SELECT * FROM food WHERE name = $pid");
    //mysql_query($result,$con);
    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {


            $result = mysql_fetch_array($result);

            $product = array();
            $product["name"] = $result["name"];
            $product["unit"] = $result["unit"];
            $product["calory"] = $result["calory"];
            $product["carbohydrate"] = $result["carbohydrate"];
            $product["category"] = $result["category"];


            // success
            $response["success"] = 1;

            // user node
            $response["product"] = array();

            array_push($response["product"], $product);

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

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

        // echo no users JSON
        echo json_encode($response);
    } */
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
        if (mysql_num_rows($result) > 0) {
        $result = mysql_fetch_array($result);

        $product = array();
        $product["name"] = $result["name"];
        $product["unit"] = $result["unit"];
        $product["calory"] = $result["calory"];
        $product["carbohydrate"] = $result["carbohydrate"];
        $product["category"] = $result["category"];


        // success
        $response["success"] = 1;

        // user node
        $response["product"] = array();

        array_push($response["product"], $product);

        // echoing JSON response
        echo json_encode($response);
    }

replace this with

while(mysql_num_rows($result) > 0 && ($result = mysql_fetch_array($result))) {

        $product = array();
        $product["name"] = $result["name"];
        $product["unit"] = $result["unit"];
        $product["calory"] = $result["calory"];
        $product["carbohydrate"] = $result["carbohydrate"];
        $product["category"] = $result["category"];


        // success
        $response["success"] = 1;

        // user node
        $response["product"] = array();

        array_push($response["product"], $product);

        // echoing JSON response
        echo json_encode($response);
    }

the result is array and you are not looping through it so it givesonly one element in the array


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...