Laravel query error - sql

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!

Related

EXPRESS and SQL "syntax error at or near "$""

I'm making PFP route with EXPRESS and PSQL and I have a little problem.
I get an error
Syntax error at or near "$"
Code:
if (!imgbbLink.startsWith("https://ibb.co/")) {
return res.status(400).json("Something went wrong")
}
await pool.query(
"UPDATE users SET user_profile_image = 1$ WHERE user_id = $2",
[imgbbLink, req.user.id]
)
res.json("PFP updated successfully")
I think there shouldn't be any problem?
Thanks.
You just made typo when indicated 1$ instead of $1:
UPDATE users SET user_profile_image = $1 WHERE user_id = $2

Query with doctrine 1

I try to make the following sql query from phpMyAdmin who works perfectly and return 1 result with doctrine 1 but i get an exception :
SQLSTATE[42S22]: Column not found: 1054 Champ 'MOY1100' inconnu dans
where clause. Failing Query: "select id_au FROM acteur_unite WHERE
code_unite = MOY1100 LIMIT 1"
Here the sql query who work on phpMyAdmin :
SELECT id_au FROM acteur_unite WHERE code_unite = 'MOY1100' LIMIT 1
Here my query with doctrine :
public function getId($code_unite) {
$con = Doctrine_Manager::getInstance()->connection();
$st = $con->execute("select id_au FROM acteur_unite
WHERE code_unite = $code_unite LIMIT 1");
$id = null;
// fetch query result
$data = $st->fetch(PDO::FETCH_ASSOC);
$id = $data['id_au'];
return $id;
}
Where i'm wrong ?
Thanks a lot in advance
seems you missing the quote around var $code_unite
$st = $con->execute("select id_au FROM acteur_unite
WHERE code_unite = '$code_unite' LIMIT 1");
but be careful with the use of var in sql .. you are at risk for sql injection . Then check for your framework the right way for the param_binding .. for avoid this risk
eg:
$st = $con->execute("select id_au FROM acteur_unite
WHERE code_unite = :code_unite LIMIT 1");
$st->bindParam(':code_unite', $code_unite, PDO::PARAM_STR);

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.

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();
}

A Database Error Occurred Error Number: 1054

i tried to make option delete for my CI with sql, but why its still error?
error warning:
A Database Error Occurred
Error Number: 1054
Unknown column 'sip' in 'where clause'
select * from t_label where label = sip
Filename: E:\web\rbt.new\system\database\DB_driver.php
Line Number: 330
code:
function hapus($id_user){
if($this->session->userdata('username')==TRUE) {
$name = $this->db->query("select USER_NAME from t_user where USER_ID = $id_user")->row();
$id_label = $this->db->query("select * from t_label where label = $name->USER_NAME")->row();
$this->db->query("delete from t_label, t_publisher where t_label.id_label = t_publisher.id_label and t_label.id_label = $id_label->id_label");
$this->db->delete('t_user', array('USER_ID' => $id_user));
redirect("registrasi/reg");
}
}
whats wrong with my code? really need help here
You need to quote the string. Try this:
$id_label = $this->db->query("select * from t_label where label = '" . $name->USER_NAME . "'")->row();