简体   繁体   中英

SQL Query Codeigniter Query Builder PHP

I need to query the database where the total_pago must equal the valor in the table contas_pagar

class Phpexcel_model extends CI_Model {

function get_users() {
    
    $query = $this->db->get_where("contas_pagar", array('total_pago' == 'valor'));
           
    return $query->result_array();
    
}

Database image

As the image the result should bring only the data that match the query, but it is bringing all the data from the table

According to the docs ( https://codeigniter.com/userguide3/database/query_builder.html#selecting-data ) you want the following (note == vs => ):

->get_where('contas_pagar', array('total_pago' => 'valor'))

What you have now is evaluating to

->get_where('contas_pagar', array(false))

because in PHP the string 'total_pago' is not equal to 'valor' .

Assuming the total_pago is a numeric field and valor is a variable that contains a number, your get_where statement should be:

->get_where('contas_pagar', array('total_pago' => $valor))

you can use this to inspect your query. so that we can see the whole query that is executed by the mysq/sql and so that we can check where the error occur

echo $this->db->last_query()

also alternatively you can use the custom query

$this->db->query('SELECT * FROM contas_pagar WERE total_pago=? ',[$valor])->result_array()
function get_users() {
    
    $query = $this->db->get_where("contas_pagar", array('total_pago' => 'valor'));
           
    return $query->result_array();
    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM