MySQL 1064 error in CodeIgniter? - php

I have query
INSERT INTO subscriptions ( client_id, name, group_id, type )
SELECT clients.id, 'Индивидуал', 0, 1 FROM clients WHERE clients.individual=1;
ALTER TABLE clients DROP COLUMN clients.individual;
ALTER TABLE finance_operations ADD COLUMN sub_id INT NOT NULL DEFAULT 0;
which works fine in Mysql Workbench.
But if I use it in codeIgniter code:
$this->db->simple_query($query);
I get error 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 'ALTER TABLE clients DROP COLUMN clients.individual; ALTER TABLE'
at line 4

PDO can not run more than one question one time;
This code is right:
$array = [ "query1", "query2", "query3" ];
foreach($array as $query)
$this->db->simple_query($query);

Related

How to get empty column and null columns in laravel?

I have a column with null values in it and one is empty. Now I am trying to fetch both but it shows me an SQL syntax error. Please guide me on how can i fetch both empty and null columns.
SQLSTATE[42000]: Syntax error or access violation: 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 ',,,,,,42450,42706,42962,43218,43474,43730,43994,44250,44506,44762,45018,45274,45' at line 1 (SQL: select token_id, email_state, email_event_time, created_at as email_created_at from email_tracker where transaction_id = 2856 and email_tracker.id IN (SELECT max(id) FROM email_tracker WHERE token_id IN (,,,,,,,42450,42706,42962,43218,43474,43730,43994,44250,44506,44762,45018,45274,45530,45793,46049,46305,46561,46817,47073,47329,47585,47841,48097,48353,48609,48880,49136,49392,49648,49904,50160,50700,50956,51212,51468,51724,51980,52236,52492,52748,53004,53260,53516) GROUP BY token_id) order by created_at desc)
$this->replies->where($field_name,'=', '')->orWhereNull($field_name);
Please guide me on what I am doing wrong here.
Any solution appreciated!

Laravel DB run two queries at once (SELECT/UPDATE)

I have one table which stores some records as JSON. I'm trying to increment the numeric value of a key from JSON column. Below is the query I'm executing in Laravel using DB facade.
DB::connection()
->select("
SELECT CAST(JSON_EXTRACT(consumed, '$.max_users') AS INT) INTO #tmp
FROM subscriptions
WHERE `id` = '2';
UPDATE subscriptions
SET consumed = JSON_SET(consumed, '$.max_users', #tmp+1)
WHERE `id` = '2';
");
It works perfectly ok in PhpMyAdmin however when attempting to execute it from Laravel it throws an exception, that SQL syntax is incorrect (where is it incorrect?).
SQLSTATE[42000]: Syntax error or access violation: 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 'UPDATE subscriptions SET consumed = JSON_SET(consumed, '$.max_users', ' at line 5 (SQL: SELECT CAST(JSON_EXTRACT(consumed, '$.max_users') AS INT) INTO #tmp FROM subscriptions WHEREid= '2'; UPDATE subscriptions SET consumed = JSON_SET(consumed, '$.max_users', #tmp+1) WHEREid= '2'; )
My suspicion is select() does not allow two different queries to be ran at once. However in this particular case I need them so in order to make use of #tmp variable. I could retrieve the value of first query and pass it to the second one using PHP, but for the sake of argument, how to run two DB selects at once in Laravel? 🤔
You can use a single update query to increment this value:
UPDATE subscriptions
SET consumed = JSON_SET(consumed, '$.max_users', CAST(JSON_EXTRACT(consumed, '$.max_users') AS INT) + 1)
WHERE `id` = '2';

one table data insert into different table with sum and group by in mysql and php

I would like to show total purchase followed by date from purchase table to insert into total_purchase table as in date and buy as well as view
my purchase table structure as below:
id|companyName |purchase|date
1|housedotcom |1300 |2016-1-1
2|homedotcom |1234 |2016-1-1
3|gardendotco |1000 |2016-1-2
4|landotcom |999 |2016-1-2
5|garagedotcom|5400 |2016-1-2
6|homedotcom |2600 |2016-1-2
7|housedotcom |1900 |2016-1-2
my total_purchase table as below
id|date |buy
1|2016-1-1|2534
2|2016-2-2|20890
I tried this into sql
INSERT INTO 'total_purchase'(date,buy) SELECT date, sum(purchase)
*FROM 'purchase' GROUP BY date;
And it showed in mysql result as I expected but when I tried insert new data into purchase table as same companyName with different date in mysql it says duplicate as well as in php coding sql it did not worked and showed this error
Error Processing RequestSQLSTATE[42000]: Syntax error or access violation: 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 'FROM purchaseGROUP BY date'
Any suggestions? Thanks in advance.
remove * from your query to be:
INSERT INTO total_purchase (date,buy) SELECT date, sum(purchase)
FROM purchase GROUP BY date;
For inserting value should be entered by just column by column.In Select you use * that return row more than one remove '*'
INSERT INTO 'total_purchase'(date,buy) SELECT date, sum(purchase)
FROM 'purchase' GROUP BY date;

How to update and insert with a single MySQL query?

I want to insert and update a table using one mysql query my table structure is:
student_id ,class_no, section, started_on ,ended_on
I want to update class_no and insert a new records. For that I am using this query:
INSERT INTO student_classes (student_id,class_no,section,started_on,ended_on)
VALUES (835,5,0,2013-04-09,null)
ON DUPLICATE KEY UPDATE class_no = class_no+1
but this query is only insert a new row,not updated column if I use where clause after DUPLICATE KEY UPDATE class_no = class_no+1 its give error message.
Message is:
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 'where started_on=2013-04-09'

Rename column name with php and MySql

I am trying to change the name of the column c to novaC with php and mysql. Everywhere I look seems to give the same solution but it doesn't seem to work:
if(isset($_GET["rename"])){
mysql_query("ALTER TABLE myTable
RENAME COLUMN c to novaC");
}
If I type: ALTER TABLE aaa RENAME COLUMN c to novaC directly in MySql it gives:
#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 'COLUMN c to novaC' at line 2
alter table tablename change oldColumn newColumn varchar(10) ;
Reference : Alter Table - MySQL Command
if(isset($_GET["rename"])){
mysql_query("ALTER TABLE myTable CHANGE c novaC varchar(9999)");
}
The MySQL documentation

Categories