1

this is my first question here. More then 5 years working with Wordpress, I always find the right way through the Google, but in this case I did not even know what to do, I really stacked :( So ...

I have MySQL table

CREATE TABLE IF NOT EXISTS `statsdata` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `date` date NOT NULL,
  `sid` int(11) NOT NULL,
  `pid` int(11) NOT NULL,
  `min` float NOT NULL,
  `max` float NOT NULL,
  `avg` float NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=236 ;

When I am trying to get data with $wpdb like

SELECT * FROM statsdata WHERE `date` = '2015-04-28' AND `sid` = '2' LIMIT 1

    $sql_data = "SELECT * FROM " . $data_db_name . " WHERE `date` = '".$today."' AND `sid` = '".$sid."' LIMIT 1";   
    $data = $wpdb->get_results($sql_data);      

I am getting empty result :( ... If I will make this query in phpmyadmin or directly from console it returns what I need ... Whats wrong here ?

P.S. Here is small peace of my data

INSERT INTO `statsdata` (`id`, `date`, `sid`, `pid`, `min`, `max`, `avg`) VALUES
(1, '2015-04-27', 2, 1, 200.401, 230.5, 229.451),
(2, '2015-04-27', 3, 1, 223, 235.61, 229.305),
(3, '2015-04-27', 1, 1, 232.027, 243.949, 237.988),
(4, '2015-04-28', 1, 1, 232.027, 243.949, 237.988),
(5, '2015-04-28', 2, 1, 232.027, 243.949, 237.988),
(6, '2015-04-28', 3, 1, 238.02, 243.506, 240.763)
4
  • 2
    Have you tried hard coding a MySQL string in your $wpdb-> get_results call instead of a string concatenated with vars? Would bet the MySQL string you create is malformed.
    – jrothafer
    Apr 28 '15 at 12:35
  • If clear read my question, I have wrote "If I am making this query SELECT * FROM statsdata WHERE date = '2015-04-28' AND sid = '2' LIMIT 1 directly in console I got right results"
    – Dark Hawk
    Apr 28 '15 at 15:13
  • Have you tried something simple like SELECT * FROM statsdata LIMIT 1 just to be sure your table is accessible by WP?
    – totels
    Feb 19 '16 at 0:00
  • I don't think @jrothafer is questioning the query but questioning whether your query is getting into you $sql_data variable properly
    – dg4220
    May 28 '16 at 10:51
2

It is hard to say without hands on access to your data. Either the query is actually failing or the query is not something you expect in that context.

You should enable error reporting via $wpdb->show_errors( true ) before it and check $wpdb->last_query for sanity after.

2
  • SQL in my prev comment is exactly what I am getting from $wpdb->last_query, did not try show_errors, but it seems no errors with it, it seems something with wpdb because, I am getting results directly in console.
    – Dark Hawk
    Apr 28 '15 at 15:58
  • This $wpdb->show_errors( true ) is trully life-saving! I didn't knwo about that! :) Thanks! :)
    – NoOne
    Feb 27 '16 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.