Select with argument between of two values - php

how i can select items between two values in codeigniter?
my table:
precocib
R$ 21.900,00
25.490,00
R$ 69.990,00
R$ 32.490,00
20.500,00
and my code to select this values is like this:
$this->db->where("precocib BETWEEN $faixaDe AND $faixaAte");
but visitors of my website can put value like this 10.000 or 10 or 10,000.00 and
when they put values like this i got an error
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '00 AND 17490,00' at line 3
SELECT `default_produtos`.* FROM `default_produtos` WHERE `precocib` BETWEEN 17490,00 AND 17490,00
so how i can resolve this problem??

Try this one
$this->db->where("FORMAT(REPLACE(precocib, ',', ''), 2) BETWEEN REPLACE($faixaDe, ',', '') AND REPLACE($faixaAte, ',', '')");
REPLACE(str,from_str,to_str)

Remove comma(,) from the 17490,00 AND 17490,00. I think it will be helpfull for you.

Related

Query where mySQL table with JSON column in ci's

I'm using codeigniter, and I have a list of numbers (e.g. ['1', '2', '12', '42']) on a column (tags_json) of a table names submit, and I want to check if the number (e.g. '2') is in tags_json, in order to show the data the row's content.
The code I've tried is:
$this->db->select('*', FALSE);
$this->db->where("JSON_CONTAINS(tags_json,'[2]'");
$Query = $this->db->get('submit');
echo $Query->num_rows();
Result is:
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 3
SELECT * FROM submit WHERE JSON_CONTAINS(tags_json,'[2]' IS NULL
Filename: controllers/Home.php
Line Number: 281
How is it in CI's?
I've found a solution to your problem. use this code...
$this->db->select('*');
$this->db->where("JSON_CONTAINS(tags_json, JSON_ARRAY('2'))");
$Query = $this->db->get('submit');
echo $Query->num_rows();
it will output 1 and 0 if nothing's found.
if it will give you this error
Invalid JSON text in argument 1 to function json_contains: "Invalid value." at position 1.
SELECT * FROM submit WHERE JSON_CONTAINS(tags_json, JSON_ARRAY('2'))
it is because your json array is invalid so make sure your to have this format
["1", "2", "12", "42"]
or you just can have like this [1, 2, 12, 42] then call JSON_ARRAY(2)
hope that helps.
Since your server does not support the JSON family of functions JSON_CONTAINS you can try a good old LIKE statement
$this->db->where('tags_json LIKE "%\'2\'%"');

why query buider codeigniter is not working?

When I click the save button on the input form, it will run the create method,
And trying to get the data in the database, but it does not work.
Error result, like this:
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near 'E 0001) AS labels FROM buku WHERE id_judul = '1'' at line 1
SELECT IFNULL(MAX(label_buku), 500 Kin E 0001) AS labels FROM buku
WHERE id_judul = '1'
Filename: models/Buku_model.php
Line Number: 33
Model :
$label = $judul->klasifikasi.' '.substr($judul->penulis,0,3).' '.substr($judul->judul_buku,0,1).' '.'0001';//500 Kin E 0001
$id_judul = $input->id_judul; //1
$label_buku = $this->db->select("IFNULL(MAX(label_buku),$label) AS labels", false)
->where('id_judul',$id_judul)
->get($this->table)->row();//error
Please help me...
500 Kin E 0001 needs to be quoted:
SELECT
IFNULL(MAX(label_buku), "500 Kin E 0001") AS labels
FROM buku
WHERE id_judul = '1'
So in your CodeIgniter query:
$label_buku = $this->db->select("IFNULL(MAX(label_buku),\"$label\") AS labels", false)
->where('id_judul',$id_judul)
->get($this->table)->row();

Query Builder number format codeigniter

I am trying to display the total price in jtable using format concat & number format, like:
Rp 1.000.000
but i got an error
<div id="container">
<h1>A Database Error Occurred</h1>
<p>Error Number: 1064</p><p>You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as JUMLAH
FROM (`retribusi`)' at line 1</p><p>SELECT `ID_KATEGORI_RETRIBUSI`, `NAMA_KATEGORI_RETRIBUSI`, `TANGGAL`, CONCAT('Rp ', FORMAT(JUMLAH, `0))` as JUMLAH
FROM (`retribusi`)</p><p>Filename: C:\xampp\htdocs\swat1\system\database\DB_driver.php</p><p>Line Number: 330</p> </div>
i make query in model like this:
function get_all_retribusi()
{
$this->db->select("ID_KATEGORI_RETRIBUSI, NAMA_KATEGORI_RETRIBUSI, TANGGAL,CONCAT('$', FORMAT(JUMLAH, 2)) as JUMLAH");
return $this->db->get("retribusi");
}
but, when i try using SQL Query PHPmyadmin i get the data
like:
anyone can help me?
SELECT `ID_KATEGORI_RETRIBUSI`, `NAMA_KATEGORI_RETRIBUSI`, `TANGGAL`,
CONCAT('Rp ', FORMAT(JUMLAH, `0))` as JUMLAH
has a stray backtick ('`')
I think you want this instead:
SELECT `ID_KATEGORI_RETRIBUSI`, `NAMA_KATEGORI_RETRIBUSI`, `TANGGAL`,
CONCAT('Rp ', FORMAT(JUMLAH, `0)) as JUMLAH
Turns out there is a codeingiter setting for this:
$this->db->_protect_identifiers=false;

my column name has parenthesis because of which when i call insert in php it gives error

$insert = "INSERT INTO state( state, sanction_percentage,
lt`(`1p`)`min, lt`(`1p`)`, lt`(`3p`)`min, lt`(`3p`)`, ht`(`415`)`min,
ht`(`415`)`, ht`(`11`)`min, ht`(`11`)`, tarrif, per_day_gen,
generation_limit ) VALUES('$state', '$sanction_percentage', '$lt1p_m',
'$lt1p', '$lt3p_m', '$lt3p', '$ht415_m', '$ht415', '$ht11_m', '$ht11',
'$tarrif', '$per_day_gen', '$generation_limit' )";
I have used escape on parenthesis but it gives error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '`(`1p`)`min, lt`(`1p`)`, lt`(`3p`)`min, lt`(`3p`)`, ht`(`415`)`min, ht`(`415`)`,'
You should escape your query column names using `
So you need to have something like this if column name is like: lt(1p)min
$insert = "INSERT INTO state( state, sanction_percentage, `lt(1p)min`, `lt(1p)`, `lt(3p)min`, `lt(3p)`, `ht(415)min`, `ht(415)`, `ht(11)min`, `ht(11)`, tarrif, per_day_gen, generation_limit ) VALUES('$state', '$sanction_percentage', '$lt1p_m', '$lt1p', '$lt3p_m', '$lt3p', '$ht415_m', '$ht415', '$ht11_m', '$ht11', '$tarrif', '$per_day_gen', '$generation_limit' )";

SQL syntax error in mysql

I'm inserting value to my MySQL table from php as:
$journey = $_POST['way'];
$from = $_POST['from'];
$to = $_POST['to'];
$dpdt = $_POST['dp_date'];
$rtdt = $_POST['rt_date'];
$fare = $_POST['fare'];
$sql = "insert into tours set " .
"journey='$journey', from='$from', to='$to', dp_date=CAST('$dpdt' AS DATE), " .
"rt_date=CAST('$rtdt' AS DATE), fare='$fare'";
on trying echo for $sql I'm getting output as:
insert into tours set journey='round', from='Aurangabad', to='Kashmir', dp_date=CAST('27-08-2013' AS DATE), rt_date=CAST('21-08-2013' AS DATE), fare='2500'
but I'm continuously getting the same error message:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from=Aurangabad, to='Kashmir', dp_date=CAST('27-08-2013' AS DATE), rt_date=CAST(' at line 1
even if I try to remove ' around the values of column names.
I'm using the same syntax for inserting data and that's working fine.
What's wrong with this?
Why MySQL does not give a proper error for such terrible mistake?
`from`='$from', `to`='$to'
FROM is reserved word use backtick around it.
FROM is reserved keyword and you should not use it. Refer Here
'from' and 'to' are reserve words
Try to do like this
[from] = 'Aurangabad', [to] ='Kashmir'
FROM is a SQL-Keyword. You must not use that without delimiters as a column name.

Categories