Select query in sql not retrieving the value in database - php

If i have employee_id with integer (like 1001) alone, am getting the answer
example the code like this ,
$claimprocess = Employee::find()
->where("employee_id = '1004' and importcompany_id = 1")
->andwhere(" status != 'Deleted' and relationship = 'Self'")
->all();
if i have a employee_id with combination interger and character, am not able get a answer(like E1004),
example code like below,
$claimprocess = Employee::find()
->where("employee_id = 'E1004' and importcompany_id = 1")
->andwhere(" status != 'Deleted' and relationship = 'Self'")
->all();
When I execute this code, am getting error like below
Exception (Database Exception) 'yii\db\Exception' with
message 'SQLSTATE[42S22]: Column not found: 1054 Champ
'E1004' inconnu dans where clause The SQL being executed
was: SELECT * FROM employee WHERE (employee_id = E1004 and
importcompany_id = 1) AND ( status != 'Deleted' )'
UPDATED:
actually am getting that value(E1004) from another variable, I use the variable instead of value, for understanding purpose I have used a value there in my question

You need to enclose your employee_id value i.e. E1004 within quotes its because it contains string literals. So your query looks like a
->where("employee_id = 'E1004' and importcompany_id = 1")

String literals in SQL are denoted with single quotes ('). Without them, the database would interpret E1004 as a column name, and fail the query, since your table doesn't have such a query.
$claimprocess = Employee::find()
->where("employee_id = 'E1004' and importcompany_id = 1")
->andwhere(" status != 'Deleted' and relationship = 'Self'")
->all();

You need to pass the value correctly , as if now you are not passing the value correctly , hence Yii is not generating Sql query correctly. You can pass second parameter to to where clause
$claimprocess = Employee::find()
->where("employee_id = ':employee_id' and importcompany_id = 1" , array(':employee_id'=>'E1004'))
->andwhere(" status != 'Deleted' and relationship = 'Self'")
->all();

Related

Yii framework migrate

So, basically what i want is to make a file that will make some changes to my database. I have to enter some data, but to be faster, I want to use the sql IN operator. So what I want should look something like this:
$this->update('basicInfo', array('regionId' => 1),
'WHERE countyId IN (SELECT id FROM countyTable WHERE regionId = 1 )')
regionId should be set to 1 when the countyId is 3,6,7,9,4 and so on
I know this won't work but I don't know how to make it work and is it possible to make it work.
See CDbCommand where function doc for more information about how to use sql 'in' criteria.
I would do it like this:
// retrieve county ids for regionId = 1
$dbConn = $this->getDbConnection();
$countyTableIds = $dbConn->createCommand()
->select('id')
->from('countyTable')
->where('regionId = 1')
->queryAll();
// prepare condition as array.
$condition = array('in', 'countyId', $countyTableIds);
// update
$this->update('basicInfo', array('regionId' => 1), $condition);
Use the ANY clause
UPDATE mytable
SET status = 'inactive'
WHERE countyId = ANY (SELECT id FROM countyTable WHERE regionId = 1 )

Cannot compare date in SQL statement

I'm using Joshcam's PHP Mysqli Database Class (github) and have been fighting over comparing dates for a long while now.
Bottom line is could anyone explain this error message:
PHP Fatal error: Problem preparing query (SELECT * FROM jobs WHERE business_id = 5 AND active = 1 AND date_visit > `2015-01-01 05:02:14` ORDER BY date_appt DESC) Unknown column '2015-01-01 05:02:14' in 'where clause'
My query goes:
$jobs = $db->rawQuery("SELECT * FROM jobs WHERE business_id = $business_id AND active = 1 AND date_visit > `".$search_from."` ORDER BY date_appt DESC");
Why would my date input be considered a column instead of a field value?
I've tried double quotes, single quotes, no quotes, and it's either turning the quotes to ' or put off by the space between Y-m-d and H:i:s.
You are using backticks for query variable rather single quote so try to remove backticks from value($search_from) else it will be treat as column
$jobs = $db->rawQuery("SELECT * FROM jobs WHERE business_id = '$business_id' AND active = 1 AND date_visit > '$search_from' ORDER BY date_appt DESC");
Enclose the names of database objects (databases, tables, fields, indexes, triggers a.s.o.) in backquotes (``). This is usually not needed but it is useful (read "required") when the name is a MySQL reserved word.
Enclose the string literals in apostrophes ('2015-01-01 05:02:14') or quotes ("2015-01-01 05:02:14"):
SELECT *
FROM `jobs`
WHERE `business_id` = 5
AND `active` = 1
AND `date_visit` > '2015-01-01 05:02:14'
ORDER BY `date_appt` DESC
None of the field names in the query above is a MySQL keyword, there is no need to enclose them in backquotes. I only did it for explanatory purposes.
Your PHP code should read:
$jobs = $db->rawQuery("SELECT * FROM jobs WHERE business_id = $business_id AND active = 1 AND date_visit > '".$search_from."' ORDER BY date_appt DESC");
The issue was mainly that this class doesn't allow for
WHERE the_date > $date1
AND the_date < $date2
I actually had to use between to solve it.
My final code:
$params = array($business_id,1,$search_from,$search_to,$limit_end);
$jobs = $db->rawQuery("SELECT * FROM jobs WHERE business_id = ? AND active = ? AND date_visit BETWEEN ? AND ? ORDER BY date_appt DESC, id DESC LIMIT ?",$params,false);

Laravel Order By certain value

I'd like for one of my relationships to return ordering by a certain value as follows:
return $this->hasMany('Photo', 'owner_id')->where('active', '=', '1')->where('visible', '=', '1')->orderByRaw("(`id` = ?) DESC", array($showphoto));
But the above code returns this :
select * from `photos` where `photos`.`owner_id` = '4' and `active` = '1' and `visible` = '1' order by (`id` = '') DESC
How do I get the $showphoto variable to be in the order by?
It won't show you that it was injected since you are not currently triggering the query.
When you will chain the get() method to it, it will "inject" the $showphoto and execute the query.
I am not aware of a way you can see the full query including the values of the parameters before actually executing the query.

mysql update clause is being ignored

I have a table with a date, ID1, ID2 score1 and score2. I am trying to update one row through php when the user has entered the specific date and id's in an html form. I have validated all the user entries. When I run my query it updates all rows in the table instead of just one. Here is a snippet of my code:
include('connect.php');
$q = mysqli_query($dbc, "SELECT * from Game");
while ($row =mysqli_fetch_array($q, MYSQLI_ASSOC))
{
mysqli_query($dbc, "UPDATE Game SET score1 ='".$points1."', score2='".$points2."'
WHERE '".$date."' = '".$row['Date']."' AND '".$id."' = '".$row['ID1']."' ");
}
mysqli_close($dbc);
Shouldn't your where clause be:
WHERE Date = '".$row['Date']."' AND ID1 = '".$row['ID1']."' ");
instead of
WHERE '".$date."' = '".$row['Date']."' AND '".$id."' = '".$row['ID1']."' ");
In the latter you - probably by accident - use some variables as field names as well, which might cause some unwanted issues. For example in your case it's likely to cause an identity condition in your where clause, which will result that all of your rows will get updated.
The WHERE clause in your update statement references only literals. There are no column references.
For debugging, I suggest you concatenate your SQL into a string, and echo it out.
$sql = "UPDATE Game SET score1 ='".$points1."', score2='".$points2."'
WHERE '".$date."' = '".$row['Date']."' AND '".$id."' = '".$row['ID1']."' ";
echo $sql;
mysqli_query($dbc, $sql);
With the values of these variables set as shown here:
$date = 'fee'
$row['Date'] = 'fi'
$id = 'fo'
$row['ID1'] = 'fum'
The WHERE clause in the generated UPDATE update statement would look like this:
WHERE 'fee' = 'fi' AND 'fo' = 'fum'
MySQL sees the values enclosed in single quotes as string literals. There's nothing wrong with that in terms of SQL (it's valid syntax) but it's much more likely you want the WHERE clause to be more like this:
WHERE fee = 'fi' AND fo = 'fum'
with fee and fo as references to columns.

Define ORDER in sql query

I'm having trouble getting my sql to pull in photos based on the column "order" which contains numbers. The following works fine, however it seems to be pulling in photos based on the "num" column in "cms_uploads".
$photoSql = "SELECT * FROM cms_uploads WHERE ('tableName'='residential_master_rental_availabilities' AND 'recordNum' = '".$rent['num']."' AND 'fieldname' = 'image') LIMIT 1";
This query returns nothing:
$photoSql = "SELECT * FROM cms_uploads WHERE ('tableName'='residential_master_rental_availabilities' AND 'recordNum' = '".$rent['num']."' AND 'fieldname' = 'image') ORDER BY 'order' LIMIT 1";
order is a reserved word.
You need backticks rather than single quotes for the order by. You are ordering by a constant -- that is, doing nothing:
ORDER BY `order`
To help with writing code, only use single quotes for string constants and dates.

Categories