I have a variable defined, $siteID. However, Laravel is losing its value inside the query and I get an error "Undefined variable: siteID"
$directoryExternalStats = DB::table('directories')
->leftJoin('citations', function($join)
{
$join->on('directories.id', '=', 'citations.directory_id')
->where( function($dirindustries) use($siteID){
$dirindustries->whereRaw('id NOT IN (SELECT DISTINCT directory_model_id FROM directory_model_industry)')
->orWhereRaw('id IN
(SELECT directory_model_id FROM directory_model_industry
(SELECT industry_id FROM industry_site WHERE site_id = '. $siteID .')
;
});
});
I am declaring the variable for use inside the query, but Laravel is not recognizing it. Changing the syntax to use a placeholder does not help.
I have tried including the variable in the first closure:
$directoryExternalStats = DB::table('directories')
->leftJoin('citations', function($join) use($siteID)
{
in the second closure:
$join->on('directories.id', '=', 'citations.directory_id')
->where( function($dirindustries) use($siteID){
and in both closures:
$directoryExternalStats = DB::table('directories')
->leftJoin('citations', function($join) use($siteID)
{
$join->on('directories.id', '=', 'citations.directory_id')
->where( function($dirindustries) use($siteID){
but I get an error regardless. With Jonas's help, I noticed that including the variable in both closures results in a different error message: "Missing argument 2 for Illuminate\Database\Query\JoinClause::where()"
Is this an instance of a Laravel bug as mentioned here:
https://github.com/laravel/framework/issues/18952
or
https://github.com/laravel/framework/issues/5992
or
https://github.com/laravel/framework/issues/5466
Or am I just committing a syntax error somewhere?
Related
I have a task that needs to be displayed every type of holiday where id is odd number.And when I tried to do it in Eloquent it always gives me an error.
Query
Select holiday_type.id, holiday_type.name
From holiday_type
Where (holiday_type.id % 2) = 0;
Laravel PHP
return DB::table('holiday_type')
->select('holiday_type.id','holiday_type.name as text')
// ->where(('id%2'), '=', 0)) ** I also tried different format but still nothing
->get();
}
You can use raw expression
DB::table('holiday_type')
->select(DB::raw('holiday_type.id, holiday_type.name as text'))
->whereRaw('MOD(id, 2) = 0')
->get();
I am developing an application using Dashdb on Bluemix and nodered, my PHP application uses the call to webservice to invoke the node-red, whenever my function on PHP invokes the node to insert on table and the field GEO_ID is null, the application fails, I understand the issue, it seems the third parameter was not informed, I have just tried to check the param before and passing something like NULL but it continues not working.
See the code:
msg.account_id = msg.req.query.account_id;
msg.user_id = msg.req.query.user_id;
msg.geo_id=msg.req.query.geo_id;
msg.payload = "INSERT INTO ACCOUNT_USER (ACCOUNT_ID, USER_ID, GEO_ID) VALUES (?,?,?) ";
return msg;
And on Dashdb component I have set the parameter as below:
msg.account_id,msg.user_id,msg.geo_id
The third geo_id is the issue, I have tried something like the code below:
if(msg.req.query.geo_id===null){msg.geo_id=null}
or
if(msg.req.query.geo_id===null){msg.geo_id="null"}
The error I got is the one below:
dashDB query node: Error: [IBM][CLI Driver][DB2/LINUXX8664] SQL0420N Invalid character found in a character string argument of the function "DECIMAL". SQLSTATE=22018
I really appreciate if someone could help me on it .
Thanks,
Eduardo Diogo Garcia
Is it possible that msg.req.query.geo_id is set to an empty string?
In that case neither if statement above would get executed, and you would be trying to insert an empty string into a DECIMAL column. Maybe try something like this:
if (! msg.req.query.geo_id || msg.req.query.geo_id == '') {
msg.geo_id = null;
}
Using node-postgres, the following snippet works fine:
let shift_solutions = function (ufrom, uto) {
let cmd = 'update solutions set "user" = \''+uto+'\' where "user" = \''+ufrom+'\''
client.query( cmd, null, function (err,rslt) {
... works
but the if I change the above to:
client.query('update solutions set "user" = %2 where "user" = %1',
[ufrom,uto],
function (err,rslt) {
... fails
yields - Unhandled rejection error: syntax error at or near "%".
I suspect it might have something to do with the fact that the user field is of type 'uuid', but don't really have a clue tbh. The parameters are supplied as strings:
[ '8e479385-5692-4acc-8dd7-4630480bd17f',
'0cc0832e-1f01-40a9-aaa4-30ae8e56d708' ]
Anybody able to shed some light on what I am doing wrong here? Thanks.
Use the dollar sign for your parameters ie
client.query('update solutions set "user" = $2 where "user" = $1'
I have a query that works in psql, but when I insert it into my REST service it doesnt work. Here is my code:
exports.getProjects = function(req, res) {
'use strict';
var query = client.query('select projects.project_id, array_to_string(array_agg(distinct project_subservices.subservice_id), ',') as subservices, array_to_string(array_agg(distinct project_certifications.certification_id), ',') as certifications from projects left outer join project_subservices on projects.project_id = project_subservices.project_id left outer join project_certifications on projects.project_id = project_certifications.project_id group by projects.project_id');
query.on('row', function(row, result) {
result.addRow(row);
});
query.on('end', function(result) {
var finalResult = {};
finalResult.meta = {};
finalResult.meta.count = result.rows.length;
finalResult.projects = result.rows;
res.json(finalResult);
});
};
Here is the error I get:
/home/jason/Projects/Node/isx_server/node_modules/pg/lib/native/index.js:141
this._sendQueryWithParams(query.text, query.values, query.singleRowMode);
^
Error: Values must be an array
at Connection._pulseQueryQueue (/home/jason/Projects/Node/isx_server/node_modules/pg/lib/native/index.js:141:10)
at Connection.connection.on.clientBuilder.port (/home/jason/Projects/Node/isx_server/node_modules/pg/lib/native/index.js:241:18)
at Connection.EventEmitter.emit (events.js:93:17)
I can cut and paste this code into psql and it works find. What is the difference in the 2 environments that will cause the query not to work? How do I make this work? Thanks for the help.
You're not escaping those single quotes, so your js function things you're passing three variables instead of a single oneā¦
If memory serves, js allows to use double quotes as string delimiters, so use that instead of single quotes seeing that you're not using double quotes in the query.
Else, add backslashes where needed to escape the single quotes.
Lets say I have a prepared statement. The query that it prepares doesn't matter. I fetch the result like above (I can't show actual code, as it is something I don't want to show off. Please concentrate on the problem, not the examples meaningless) and I get
Fatal error: Call to a member function bind_param() on a non-object in... error. The error caused in the called object.
<?php
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
class table2Info{
private $mysqli;
public function __construct($_mysqli){
$this->mysqli = $_mysqli;
}
public function getInfo($id)
{
$db = $this->mysqli->prepare('SELECT info FROM table2 WHERE id = ? LIMIT 1');
$db->bind_param('i',$db_id);
$db_id = $id;
$db->bind_result($info);
$db->execute();
$db->fetch();
$db->close();
return $info;
}
}
$t2I = new table2Info($mysqli);
$stmt->prepare('SELECT id FROM table1 WHERE name = ?');
$stmt->bind_param('s',$name);
$name = $_GET['name'];
$stmt->bind_result($id);
$stmt->execute();
while($stmt->fetch())
{
//This will cause the fatal-error
echo $t2I->getInfo($id);
}
$stmt->close();
?>
The question is: is there a way to do another prepared statement while another one is still open? It would simplify the code for me. I can't solve this with SQL JOIN or something like that, it must be this way. Now I collect the fetched data in an array and loop through it after $stmt->close(); but that just isn't good solution. Why should I do two loops when one is better?
From the error you're getting it appears that your statement preparation failed. mysqli::prepare returns a MySQLi_STMT object or false on failure.
Check for the return value from your statement preparation that is causing the error. If it is false you can see more details by looking at mysqli::error.