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.
Related
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\'%"');
I'm working on a project that involves a PHP script that calls an API and gets a JSON array. I then want to put this JSON array into a MySql database. The issue I am running into is that while the script executes without any errors or exceptions in the terminal, my database is not filling with any data.
I am running MySQL Workbench as my MySQL client and have created a schema called "team_data" into which I am attempting to input my JSON array. I have removed my API key for obvious reasons. Any ideas where I am going wrong here?
<?php
$con = mysql_connect("127.0.0.1","XXXXXX","XXXXXX") or die('Could not connect: ' . mysql_error());
mysql_select_db("test1", $con);
$json = file_get_contents('team_data.json');
$data = json_decode($json, true);
foreach($data as $row)
{
$game = $data['nfl_game_id'];
$team = $data['team'];
$opponent = $data['opponent'];
$totfirstdown = $data['totalfirstdown'];
$totyds = $data['totyds'];
$pyds = $data['pyds'];
$ryds = $data['ryds'];
$pen = $data['pen'];
$penyds = $data['penyds'];
$trnovr = $data['trnovr'];
$pt = $data['pt'];
$ptyds = $data['ptyds'];
$ptavg = $data['ptavg'];
$sql = "INSERT INTO Teams(nfl_game_id, team, opponent, totalfd, totyds, pyds, ryds, pen, penyds, trnovr, pt, ptyds, ptavg);
VALUES('$game', '$team', '$opponent', '$totfirstdown', '$totyds', '$pyds', '$ryds', '$pen', '$penyds', '$trnovr', '$pt', '$ptyds', '$ptavg')";
mysql_query($sql,$con);
}
?>
Error from your comment, after I suggested you check for errors on your query:
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 '; VALUES('', '', '', '', '', '', '', '', '', '', '', '', '')' at line 1
The error shows you where it starts right syntax to use near '; < right there.
... ptyds, ptavg); < see that semi-colon? Remove it. It's an end of statement character.
However, you're doing foreach($data as $row) but not using $row.
You need to change all $data['xxx'] to $row['xxx'] which is why your values are empty.
If there are any characters that MySQL will complain about, then you will need to escape your data. Any which way, it's best that you do.
As a bonus answer:
Your present code is open to SQL injection. Use mysqli_* with prepared statements, or PDO with prepared statements.
First let's see whether your command successfully retrieved the JSON data.
var_dump($data);
Let's place that right after the line where we json_decode the data.
If the JSON data looks good in our array, then the next thing for us to check would be the SQL (maybe there are required columns that aren't receiving values or other constraint, etc.)
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.
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.
Code:
$value["name"] = $form['name']['first'];
$contact = db_fetch_array(db_query("INSERT INTO test values('4', $value["name"])"));
error is shown here but what will be the correct code to submit $form data into a database?
Can anyone help me?
Thank you.
INSERT queries don't return a result, so there's nothing to fetch. Despite that your query is syntactically wrong: string values must be wrapped in quotes, numeric values must not.
db_query("INSERT INTO {test} VALUES (4, '%s')", $form['name']['first']);
As of Drupal 6 db_query("INSERT ...") is discouraged. drupal_write_record() should be used instead.
$record = array('id' => 4, 'firstname' => $form['name']['first']);
drupal_write_record('test', $record);
For more information please refer to the docs.