IN clause is not working properly in MYSQL php - php

I have a sql query like this
$statusData = $DB->get_records_sql("SELECT id, element, value FROM {scorm_scoes_track} WHERE scormid = ? AND userid = ? AND element =$DB->get_in_or_equal('cmi.core.lesson_status','cmi.core.total_time')", array($scorm_id, $userid) );
So i am trying to fetch the values from query
<?php
$statusData = reset($statusData);
if($statusData->element == 'cmi.core.lesson_status')
$status = $statusData->value ;
elseif($statusData->element == 'cmi.core.total_time')
$totaltimes = $statusData->value ;
but its just showing only status value..it's not showing total time value..Can any one help me
Thanks in advance..

You do not have any in operator in your code, so there is nothing that could work at all as an in!
Replace the = with in and remove the moodle call as well as shown below:
...AND element IN ('cmi.core.lesson_status','cmi.core.total_time')

Related

How to add Distinct function to a array or return only unique values

How to add distinct function so that the result is not repetitive?
Any help is much appreciated.
$yearnow=$_POST['yearnow'];
$stmts = $db->query("select * from tblgencol where year='$yearnow'");
while($row= $stmts->fetch(PDO::FETCH_OBJ)){
$pdf->Row(Array($row->busname,$row->business));
}
}
Here is the output of my code:
I'm using fpdf to display my data from database. And its working fine. Its just that the name is repeating.
This is what I want to achieve:
or what if i want to display this?
option 2
To achieve your desired result, you need to check if the current business name is the same as the last displayed one, and if so, not output it:
$yearnow=$_POST['yearnow'];
$stmts = $db->query("select * from tblgencol where year='$yearnow'");
$busname = '';
while ($row = $stmts->fetch(PDO::FETCH_OBJ)) {
$thisbusname = ($row->busname != $busname) ? $row->busname : '';
$pdf->Row(array($thisbusname,$row->business));
$busname = $row->busname;
}

transform sql query to Fluentpdo query

i have this sql query written in php:
$query = sprintf("UPDATE bank_info SET
amount_dollar = amount_dollar +'$amount_dollar' ,
amount_euro = amount_euro + '$amount_euro' ,
amount_local = amount_local + '$amount_local'
WHERE bank_id = '$bank_id' ");
this query works fine, but i want to transform this query using FluentPDO.
i want to use arrays to SET the values .
for example:
$table='bank_info'; //table name
$arrVal=array(); //values needs to be SET
$arrVal['amount_dollar = amount_dollar+?']=$amount_dollar;
$arrVal['amount_euro = amount_euro+?']=$amount_euro;
$arrVal['amount_local = amount_local+?']=$amount_local;
$arrWhere=array(); //where condition
$arrWhere['bank_id']=$bank_id;
this is the query:
$query = $this->pdo->update($table)->set($arrVal)->where($arrWhere);
$query->execute();
I think the problem is in the $arrVal, cant find the proper way to SET and add value to the current value for a column in the table.
I used array to select and get values from the DB/tables for many times so i think the $arrWhere is not the problem.
well, found the answer,
for ex.:
This is working for me:
$id = 5;
$field = 'stock';
$amount = 1;
$increment = array($field => new FluentLiteral($field.' + '.$amount));
$fpdo->update('products')->set($increment)->where('id', $id)->execute();

Laravel MySQL with where conditions avoid columns

I want to perform following search query, when user sends values I want to get matching results
If any results contain "none" i want to remove it from where condition and check only matching values for others
Example 1
$fructose = high
$lactose = mid
$polyols = none
$fructan = none
if so i want to check only matching results for $fructose=high and $lactose=mid
Example 2
$fructose = high
$lactose = mid
$polyols = mid
$fructan = mid
If so I want to check all matching results
Please advise, below is my current query
$fod = FodMap::select('*')
->where('fructose_level', '>=', $fructose)
->where('lactose_level','>=', $lactose)
->where('polyols_level', '>=', $polyols)
->where('fructan_level', '>=', $fructan)
->get();
Here I tried to solve your problem by writing query using eloquent structure. You should manage value and operator in where condition as per your need.
You can write your query with if condition like this:
$query = FodMap::select('*');
if($fructose != none){
$query = $query->where('fructose_level',$fructose);
}
if($lactose != none){
$query = $query->where('lactose_level',$fructose);
}
if($polyols != none){
$query = $query->where('polyols_level',$fructose);
}
if($fructan != none){
$query = $query->where('fructan_level',$fructose);
}
$fod = $query->get();

Get the value of a cell in mysql table

I'm using CodeIgniter to write a few lines of simple code to get value from the mysql table.
What I've tried:
$interview_id=45
$category_raw = $this->db->query("select category from interview where interview_id='$interview_id'");
$category = $category_raw->first_row();
But for some reason, the $category is shown as "array()", while I actually want to get the value within.
Any advice on how to proceed?
Thanks so much,
Try
$interview_id=45;
$category_raw = $this->db->query("select category from interview where interview_id=$interview_id");
$category = $category_raw->first_row()->category;
Or with active record
$interview_id=45;
$this->db->select("category");
$this->db->where("interview_id",$interview_id);
$category_raw = $this->db->get("interview");
$category = $category_raw->first_row()->category;
You can take the value like
$category = $category_raw->first_row()->category;
because first_row() alwise return value as object
documentation

Check if databasefield contains value from imploded variable

$users = explode(",", $particiTemp);
foreach($users as $user) {
echo "$user";
}
$checkSQL = mysql_query("SELECT * FROM this WHERE x>'$y' && v<'$z' && user IN ($particiTemp)");
while($checkData = mysql_fetch_array($checkSQL)){
}
I´m kinda stuck right here...
I want to check if $particiTemp (for example: $particiTemp = "2,23,11,4,") is in the field $checkData[user] ($checkData[user] = "5,22,11,23";).
I tried to explode both and tried mysql IN but I don´t know how to check if the field $checkData[user] CONTAINS one of the imploded $particiTemp
"In" clause need values as '2','23','11','4' instead of "2,23,11,4,"
so manipulate your array for result.
$innval = '';
foreach($users as $user) {
if($user != '')
$innval .= "'".$user."',";
}
$innval = substr($innval,0,-1); // to remove last extra ,
and now use this $innval variable in your query to get result
$checkSQL = mysql_query("SELECT * FROM this WHERE x>'$y' && v<'$z' && user
IN ($innval)");
Hope this helps
Idea: explode array to check, for each a mysql_query with INSTR.
Should work, trying it now.

Categories