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\'%"');
Related
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();
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;
I am using CodeIgniter to insert a new record to an Oracle table in php. THe code is on the below:
public function insert_street_file( $insdata = null )
{
if ($insdata == null)
{
// return false;
}
else
{
$insdata = array(
'NAME'=>"Eray",
'SURNAME'=>"BAlk",
'CITY'=>"HALIFAX",
'PROVINCE'=>"NS",
'LSTNOLO'=>1,
'LSTNOHI'=>2,
'RSTNOLO'=>3,
'RSTNOHI'=>4,
'POSTCODE'=>"b3h1s8",
'POSTDIR'=>"1s8",
'PREDIR'=>"b3h",
'STR_CODE'=>30002
);
$results =$this->oracle_db->insert("$this->street_file_table_name", $insdata);
return $results;
And i am getting the error below:
Error Number:
INSERT
INTO People
("NAME", "SURNAME", CITY", "PROVINCE", "LSTNOLO", "LSTNOHI",
"RSTNOLO", "RSTNOHI", "POSTCODE", "POSTDIR", "PREDIR", "STR_CODE")
VALUES ('Eray', 'Balk', 'Halifax', 'NS', 1, 2,
3, 4, 'b3h1s8', '1s8', 'b3h', 30002)
Filename: C:\inetpub\ci-2-1-3-system\database\DB_driver.php
Line Number: 330
There is no error number specified, so I don't know what exactly the problem is.
DB connection is provided, any select query works well.
$this->street_file_table_name value is correct
column names are also in capital letters in the Oracle db. All column names and table name are good.
I appreciate if you help me solve this issue.
Edit: The missing quotes were typos. It is not the real problem.
You have syntax error. Appostrophe missing
use
'NAME'=>"Eray",
instead of
'NAME=>"Eray",
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.
So I am working with a very old site where the client wont let me upgrade it to any sort of PDO. Anyway I have to use mysql querys and its really starting to depress me. I cant seem to get this to work:
$array = array('1' => 1, '2' => 2);
$values = "({$array['1']}, 2, 'data3', 4, 5)";
if ($moreData){
$values = $values.", ({$array['6']}, 7, 'data8', 9, 10)";
}
$this->db->query("INSERT INTO " . DB_PREFIX . "customer_reward ".
"('field1','field2','field3','field4','field5') ".
"VALUES ".$values);
Any help would be greatly appreciated.
UPDATE:
Here Is The Error Message:
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 ''field1','field2','field3','field4','field5') VALUES (1, 2, 'data3' at line 1<br />Error No: 1064<br />INSERT INTO table ('field1','field2','field3','field4','field5') VALUES (1, 2, 'data3', 4, 5)
The field names in your query should not be within single quotes. They either need to have no quotes or be inside back-ticks.
You also need to make sure the field types match the data types as you are trying to enter in some integer values.