• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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
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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.