Welcome Guest, Not a member yet? Register   Sign In
Select Query Error
#1

Hi everyone, i have a problem with a select query, 


Code:
$where = "SELECT FORMAT(SUM(payed), 2) AS payedAmount FROM client_credit WHERE creditStatus = 2 AND idperson = '"
           .$post_data['idperson']. "' AND contractDate
       BETWEEN '"
           . $startDate . "' AND '" . $endDate . "'";
$query = $this->db->query($where);
       if ($query->num_rows() > 0) {
           return $query->result();
       } else {
           return null;
       }

But i get a boolean result, $query = false, and when i execute the same query in mysql console i get the right result, but here i get boolean.

Thank you. Confused
Reply
#2

Did you add an echo $where; to get the generated SQL or did you just guess what it actually generated and run it in MySQL console?

Also, your code are vulnerable to SQL Injection, you should fix that ASAP!
https://www.codeigniter.com/user_guide/d...y-bindings
https://en.wikipedia.org/wiki/SQL_injection
Reply
#3

Insert these 2 lines just for testing purposes after $query = $this->db->query($where);
PHP Code:
echo $this->db->last_query();
die(); 

This will echo the query like it's being sent to MySQL.
Check if this is what you expected.
Reply
#4

(This post was last modified: 06-12-2019, 01:51 PM by hc-innov.)

are you sure your idperson is a string in your database??
change:

PHP Code:
$where "SELECT FORMAT(SUM(payed), 2) AS payedAmount FROM client_credit WHERE creditStatus = 2 AND idperson = '"
 
          .$post_data['idperson']. "' AND contractDate
       BETWEEN '"
 
          $startDate "' AND '" $endDate "'"
to
PHP Code:
$where "SELECT FORMAT(SUM(payed), 2) AS payedAmount FROM client_credit WHERE creditStatus = 2 AND idperson = "
 
          .$post_data['idperson']. " AND contractDate
       BETWEEN '"
 
          $startDate "' AND '" $endDate "'"
Reply




Theme © iAndrew 2016 - Forum software by © MyBB