0

When i connect with mysql ,i get this value { id: 14 } actually i wanna get only 14 this is my code

app.get('/register',function(req,res){
    var data = {
        "error":1,
        "result":""
    };

    console.log("ams");
    connection.query("SELECT id from register",function(err, rows, fields){
        if(rows.length != 0){
            data["error"] = 0;
            data["result"] = rows;
            res.json(data);
            console.log(data.result[0]);
        }else{
            data["result"] = 'No data Found..';
            res.json(data);
        }
    });
});
6
  • Do you expect only one result? Commented Feb 10, 2016 at 12:44
  • yes sure i wanna get value 14 Commented Feb 10, 2016 at 12:47
  • yes, but it is simply the json data notation... can't you just read the json and see what is the value of the key "id"? Commented Feb 10, 2016 at 12:48
  • the value is 14 but i do not want to get the key every time i just want the value to access it later Commented Feb 10, 2016 at 12:50
  • dear @amrhashem, the Michael's solution you have accepted, is the same thing I have written here. Commented Feb 10, 2016 at 13:31

1 Answer 1

1

If you're expecting one row only, you could set

data["result"] = rows[0].id;

However, your response is an array of json objects no matter how many results you get. It's much better to setup the res.json() receiver to work with objects, not plain strings/numbers.

It is not possible to get only values as a mysql query result in node with node-mysql (and I don't think there's any other library that would do that, because it doesn't make sense).

Sign up to request clarification or add additional context in comments.

2 Comments

i get this value { "error": 0, "result": 14 } but i want just the value not the key
Then why do you set it in the first place? Return res.json(rows[0].id) if you're only interested in the value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.