PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 20, 2022

[FIXED] MySQL query missing results in node-mysql that are returned in phpMyAdmin

 January 20, 2022     ajax, mysql, node.js, phpmyadmin     No comments   

Issue

My SQL query returns four columns when I run it in phpMyAdmin, but only two when performed through Node.js and the mysql package. I suspect it might have to do with the asynchronicity or with LEFT JOIN, but I can't figure it out.

Node index.js:

app.get('/name', function (req,res){
  var query='SELECT s.result, s.distance, hp.result, hp.distance FROM `table15` s LEFT JOIN `table14` hp on s.name = hp.name WHERE s.name = "John Appleseed"';
  pool.query(query,function(err, result) {
    if (err) throw err;
    console.log(result);
    res.json(result);
  });
});

app.js

  $.ajax({
      url : "/name",
      type : "GET",
      success : function(data){
          var len = data.length;
          console.log(data);
      }
  });

Returns in browser and console:

[{result: "36:24", distance: 12}]

But in phpMyAdmin I get four columns and would therefor expect something like this in the browser as well:

[{result: "00:35:29", distance: 12, result: "36:24", distance: 12}]

Solution

Having zero familiarity with node.js i would suggest the following. Try altering your query and add names to your result columns.

Example

SELECT `s`.`result` AS `sResult`, 
`s`.`distance` AS `sDistance`, 
`hp`.`result` AS `hpResult`, 
`hp`.`distance` AS `hpDistance` FROM `table15` `s` 
LEFT JOIN `table14` `hp` on `s`.`name` = `hp`.`name` 
WHERE `s`.`name` = "John Appleseed";

The above should output

[{sResult: "00:35:29", sDistance: 12, hpResult: "36:24", hpDistance: 12}]


Answered By - user2560539
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing