Laravel Query Builder select a column with multiple values - php

I have four arrays and a one variable like below.
$jptypes = ["26","27"]
$jvalues = ["57","62"]
$jbranches = ["33","63"]
$jstream = ["2","2"]
$jmarks = 10
I want to collect employees with those matching arrays and value.
$empsarr = DB::table('otc_employee_qualifications')
->whereIn('emp_qualifctn_type',$jptypes)
->whereIn('qualification_value',$jvalues)
->whereIn('emp_qualifctn_branch',$jbranches)
->whereIn('emp_qualifctn_stream',$jstream)
->where('qualification_mark','>=',$jmarks)
->distinct()
->lists('employee_id');
return $empsarr;
But whereIn method will not checking both values. its in OR case.
I want to check both the values in the array at once.
how can i solve this issue ?

Related

Creating an associative array to query one SQL in PHP to get several results

I am new to PHP and would appreciate any assistance in creating an associative array to query a table using SQL.
I want to use one select statement to get multiple results depending on the 'WHERE' key.
$a = $DB->getOne("SELECT DEF_VAL FROM JDEFAULTS WHERE DEF_KEY = :0", array('ip'));
$stmt1 = $DB->getOne("SELECT DEF_VAL FROM JDEFAULTS WHERE DEF_KEY = :0", array('ftpUserid'));
$c = $DB->getOne("SELECT DEF_VAL FROM JDEFAULTS WHERE DEF_KEY = :0", array('ftpPasswd'));
These codes return different values (as expected). I want to merge all of these into one line of SQL and get the values in a list depending on the DEF_KEY value.
how to create associative array from sql table
I have tried following the solutions from this but haven't been able to execute the code.
#Hashibul Hussain i havent understand your questions completly, but as far as i understood you want one query with three conditions. you can try this. this may work.
$stmt1 = $DB->getOne("SELECT DEF_VAL FROM JDEFAULTS WHERE DEF_KEY = :0 or WHERE DEF_KEY = :0 or WHERE DEF_KEY = :0", array('ip'), array('ftpUserid'), array('ftpPasswd'));
please elabrate your question further so that i can come up with better solution.

How to get an array value from the query in Yii 1.1

$us= Yii::app()->db->createCommand()
->select('default_number_of_devices')
->from('user')
->where('id=1')
->queryRow();
echo "$us";
I should have got one value instead of array type because id is unique. But $us appears to be an array instead of single number.
You should use queryScalar() if you want to get single value from single column:
$us = Yii::app()->db->createCommand()
->select('default_number_of_devices')
->from('user')
->where('id=1')
->queryScalar();
echo $us;
queryRow() returns first row from query. And since row usually contains multiple columns, array is expected format (each element of array contains value of single column).

Insert array and non array values to database using a single sql statement

I have this, working, code, for inserting array to database:
$c = array_map(function ($reqNo,$officer,$product,$quantity){return "'$reqNo','$officer','$product','$quantity'";} , $reqNo,$officer,$product,$quantity);
if(!$insert = mysql_query("INSERT INTO request (RNO,UID,PID,QtyR) VALUES (".implode('),(', $c).")"))
Now, the problems is that i would like also to insert, alongside the array, none array values to the same database table, using the same sql insert statement..here's my code so far,
if(!$insert = mysql_query("INSERT INTO request (RNO,UID,PID,QtyR,Iuse,Designation,QtyA,QtyA1,QtyI,Rdate,Rtime,bar) VALUES (".implode('),(', $c).",'replacement','ICTU','-1','-1','-1',CURDATE(),CURTIME(),'1' )"))
and here's the error i'm getting:
Column count doesn't match value count at row 1
Any ideas on how to go about this?
I did get a way around it, though not sure if it's the correct way..this' how i did it:
//first, i got the length of the array
for($i=0;$i<count($product);$i++){}
//i used array_fill() to duplicate the single values into the length of the array
$new_usage = array_fill(0,$i,$usage);
$new_designation = array_fill(0,$i,$designation);
$QtyA = array_fill(0,$i,"-1");
$QtyA1 = array_fill(0,$i,"-1");
$QtyI = array_fill(0,$i,"-1");
$date = array_fill(0,$i,date("Y-m-d"));
$time = array_fill(0,$i,date("H:i:s"));
$bar = array_fill(0,$i,"1");
//then i put the above new arrays into array_map(), together with the original array
$c = array_map(function ($reqNo,$officer,$product,$quantity,$new_usage,$new_designation,$QtyA,$QtyA1,$QtyI,$date,$time,$bar){return "'$reqNo','$officer','$product','$quantity','$new_usage','$new_designation','$QtyA','$QtyA1','$QtyI','$date','$time','$bar'";} , $reqNo,$officer,$product,$quantity,$new_usage,$new_designation,$QtyA,$QtyA1,$QtyI,$date,$time,$bar);
//from there, i imploded the array_map into the sql insert statement
if(!$insert = mysql_query("INSERT INTO request (RNO,UID,PID,QtyR,Iuse,Designation,QtyA,QtyA1,QtyI,Rdate,Rtime,bar) VALUES (".implode('),(', $c).")")){
...
I still don't know if this' the right way to go about it, but all in all, it did work.

PHP Match two arrays on key value (like mysql join)

Is there a way to join two arrays based upon a same value in a key?
As an example in MySQL you can left join two tables when two fields have the same value in it.
The first array called 'phoneArr' is one with a person_id and a phone number
The second array called 'clientDate' is one with a person_id and a appointment date.
Here are the arrays:
$phoneArr = array();
$phoneArr[0]['person_id'] = "123456";
$phoneArr[0]['phone'] = "555-2222";
$phoneArr[1]['person_id'] = "7654321";
$phoneArr[1]['phone'] = "555-1111";
$clientDate = array();
$clientDate[0]['person_id'] = "123456";
$clientDate[0]['date_time'] = "01-07-13 13:00";
$clientDate[1]['person_id'] = "7654321";
$clientDate[1]['date_time'] = "01-07-13 10:30";
Now if the person id in clientDate will always exist in the phoneArr, but not the other wat around. The persons in the phoneArr do not always exist in the clientDate.
What I want is to get a match of these arrays where I will be left with a new array with the info of both arrays, but only of there is a match on the 'person_id'.
Is this doable without MySQL?
If you are going to look up data by a key value, and that key value is unique over the table, you might consider using that as the array key, so your $phoneArr would be set up as:
$phoneArr["123456"]['phone'] = "555-2222";
$phoneArr["7654321"]['phone'] = "555-1111";
Do the same with the other array
Then you can:
foreach ($phoneArr AS $id=>$record) {
if (array_key_exists($id,$clientDate)) {
// the client id is $id, $record has their phone #, and $clientDate[$id] has their date/time
// do something with it here - either process it (preferable) or put the data in another array.
}
}

Codeigniter Active Record Selecting Multiple Rows using Multiple Primary Keys

I'm having a problem returning multiple rows dynamically.
I have an active record query that needs to return multiple rows from the same table.
//this will be a dynamic array of ids
$array = array('01','02','03');
//i need to have other where conditionals as well
$cond['userlevel'] = 5;
//then add the array of ids to the conditionals array
$cond['id'] = implode(',',$array);
//then build the active record query
$q = $this->db->select($col->where($cond);
It only ever seems to return the first item in the array of ids.
Try something like
"SELECT * FROM table_name WHERE userlevel IN(?,?,?,?)", array(0,1,2,3);
Please try this, it will help you.
$this->db->select('*');
$this->db->from('table_name');
$this->db->where_in('column_name',array(0,1,2,3));
Note:- Make sure where_in array value should not empty otherwise it will get MySQL Error.

Categories