why does this code keep erroring on codeigniter - sql

public function semua_data1()
{
$this->db->select('*');
$this->db->from('tbl_m_stock');
$tgl = date('Y-m-d', strtotime("-365 day", strtotime(date('Y-m-d'))));
$this->db->join('tbl_t_purchase', 'tbl_m_stock.purchase_id = tbl_t_purchase.purchase_id', 'left');
$this->db->where('tbl_t_purchase.create_date <', $tgl);
$this->db->where('tbl_m_stock.stockin - tbl_m_stock.stockout >', 0);
return $this->db->get();
}
What is wrong?
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 '`tbl_m_stock`.`stockout` >0' at line 5
SELECT * FROM `tbl_m_stock` LEFT JOIN `tbl_t_purchase` ON `tbl_m_stock`.`purchase_id` = `tbl_t_purchase`.`purchase_id` WHERE `tbl_t_purchase`.`create_date` < '2022-01-26' AND `tbl_m_stock`.`stockin -` `tbl_m_stock`.`stockout` >0
Filename: D:/xampp/htdocs/SINARBANGUNAN07/system/database/DB_driver.php
Line Number: 691

Because it's 2 different fields with an operator you need to parenthesis them.
$this->db->where('(tbl_m_stock.stockin - tbl_m_stock.stockout) >', 0);
Should work

Related

Laravel query error

laravel return this error on execute this line:
$select = trim($request->select);
$where = trim($request->where);
$d = trim($request->d);
$order = trim(stripslashes($request->order));
$limit = isSet($request->limit) ? " LIMIT ".trim($request->limit) : '';
$forUser = trim(stripslashes($request->userId));
$campaignId = trim(stripslashes($request->campaignId));
$userRole = trim(stripslashes($request->userRole));
$events = DB::select('SELECT *, DATE_FORMAT(timestamp, ?) selector FROM events WHERE DATE_FORMAT(timestamp, ?) = ? AND campaignId = ? ORDER BY ? ASC ?', [$select, $where, $d, $campaignId, $order, $limit])->get();
Error:
[2018-05-21 19:09:22] local.ERROR: exception 'PDOException' with message '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 '?' at line 1' in C:\xampp\htdocs\spotlike_laravel\trunk\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php:77
Any ideas? :(
Problem solved, the variable "$limit" was empty, then on the concatenate of $limit generate a blank space on the query. Thanks darol and Phil!

Ignite Query Exception

Im getting the following error:
Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "SELECT
""standard_item_cache"".""STANDARDITEM""._KEY,
""standard_item_cache"".""STANDARDITEM""._VAL FROM
""standard_item_cache"".""STANDARDITEM"" WHERE ITEMID[*] == ? "; SQL statement:
SELECT "standard_item_cache"."STANDARDITEM"._KEY,
"standard_item_cache"."STANDARDITEM"._VAL FROM
"standard_item_cache"."STANDARDITEM" WHERE itemId == ? [42000-196]
When I try to perform a simple query:
String itemId = params.get(Params.PARAM_ITEM_ID);
SqlQuery<String, StandardItem> sql = new SqlQuery<>(StandardItem.class, "itemid == ?");
try (QueryCursor<Cache.Entry<String, StandardItem>> cursor = standardItemIgniteCache.query(sql.setArgs(itemId))) {
logger.info("publish standard items from cache");
for (Cache.Entry<String, StandardItem> entry : cursor) {
logger.info("publish standard item: " + entry.getValue().toString());
}
logger.info("publishing standard items from cache done");
cursor.close();
}
Where is the mistake? Im doint it exactly like it is described in the apache ignite examples: https://apacheignite.readme.io/v1.0/docs/cache-queries
The mistake is in this tiny string: itemid == ?.
You used == instead of =. SQL equality operator is a single =.

SQL update not running, reasons unknown

I am trying to run an update and for reasons I cannot figure out why it is not running.
The error:
Failed to run query: 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 'WHERE EventID = '2'' at line 4
I cannot figure out what is wrong with the syntax.
$query = "
UPDATE event
SET AssignedTo = '$AssignedTo',Project = '$Project',Category = '$EventCategory',
Status = '$Status',Services = '$EventServices',Priority = '$EventPriority',
WHERE EventID = '$ID' ";
try {
$stmt = $db->prepare($query);
$stmt->execute();
}
catch(PDOException $ex){ die( "Failed to run query: " . $ex->getMessage()); }
die("                      Changes Submitted");
}
This requires formatting the text, so it is too long for a comment.
If you format your queries neatly, then you can avoid or at least minimize such problems:
UPDATE event
SET AssignedTo = '$AssignedTo',
Project = '$Project',
Category = '$EventCategory',
Status = '$Status',
Services = '$EventServices',
Priority = '$EventPriority',
-------------------------------------^
WHERE EventID = '$ID';
The lines for the query don't scroll off the page, making it easier to spot an extra comma.

Near "*": syntax error Unable to execute statement

I'm trying to execute an sqlite code that delete all values from the table 'Categories"
Here is My code:
function deleteCATs() {
var db = LocalStorage.openDatabaseSync("Cat", "1.0", "Deleting Categories File", 1000000);
db.transaction(
function(tx) {
tx.executeSql('DELETE * FROM Categories');
//tx.executeSql('DELETE FROM Categories WHERE Category ="Cat"');
}
)
}
I'm getting the following error:
"Near "*": syntax error Unable to execute statement"
Any ideas on how this can be fixed? I'm using Qt Creator 3.0.0, Qt 5.2.0 Android version
The syntax is:
DELETE FROM Categories;
(The * is not necessary.)
Or, better yet in almost any database other than SQLite:
TRUNCATE TABLE Categories;

SQL error in YII ::: SQLSTATE[42000]: Syntax error or access violation: 1064

I Wrote a sql code in yii and its giving this error:
CDbCommand failed to execute the SQL statement: 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 ')' at line 1. The SQL statement executed was: SELECT * FROM users where id in ()
I had no idea why this is happening.........
The code is:
$t = implode(",", $array12);
echo $t;
$sql2 = 'SELECT * FROM users where id in ('. $t. ')';
// echo $sql2; die;
$command = $connection->createCommand($sql2);
$row5 = $command->queryAll();
echo "<pre>";
print_r($row5);
echo "</pre>";
When I echo the sql using echo $sql2 and die() to see the sql, it gives me this:
SELECT * FROM users where id in (44,45)
Now, I used above sql directly in the the as
$sql2 = 'SELECT * FROM users where id in (44,45)';
$command = $connection->createCommand($sql2);
$row5 = $command->queryAll();
and its work perfectly, I do not know what to do with my sql.
You got error when $array12 is empty:
SELECT * FROM users where id in () (check whole sql at the end of a error's message)
You have to check to count elements in $array12:
if (count($array12)) {
$t = implode(",", $array12);
$sql2 = 'SELECT * FROM users where id in ('. $t. ')';
// echo $sql2; die;
$command = $connection->createCommand($sql2);
$row5 = $command->queryAll();
} else {
$row5 = array();
}