$query->where plus AND operator - php

From Joomla documentation i read of how to run sql queries in php.
My code is the below:
$query->select($db->quoteName('customfield_value'));
$query->from($db->quoteName('#__virtuemart_product_customfields'));
$query->where($db->quoteName('virtuemart_custom_id')." = ".$db->quote('322'));
$query->and($db->quoteName('virtuemart_product_id'). " = ".$item->virtuemart_product_id));
$db->setQuery($query);
$results = $db->loadObject();
echo "Factory Code:" . $results->customfield_value;
Code was bringing a Factory code but not product's Factory code and then i had to add the line
$query->and......
to tell to bring Factory code from specific product but it brings no results at all. Eventually i write something wrong with $query->and ?
Any ideas/solutions?
Thank you in advance

Ok i found correct answer/syntax:
$query->select($db->quoteName('customfield_value'));
$query->from($db->quoteName('#__virtuemart_product_customfields'));
$query->where($db->quoteName('virtuemart_custom_id')." = 322", "AND")
->where($db->quoteName('virtuemart_product_id')." = ". $item->virtuemart_product_id);

Related

SQL-PHP error solution

Evening community,
I stuck at one problem, which I can't find solution for. I would appreciate if you could give me a piece of your advice.
In brief, I wrote the following function in a php-file:
public function getCurrencyReal(){
$sql = "SELECT currency_real FROM currency WHERE currency_id = '4' limit 1";
$query = $this->db->query($sql);
$currency_real = $query;
return $currency_real->row;
}
After that I added the following code in another php file, that should generate XML-list:
$currency_real = $model_module_xmlcreator -> getCurrencyReal();
and
$out .= "<test>" . $currency_real . "</test>";
As a result I've received the following thing:
, saying "Array".
I've realized that I'm asking for an array even though I want to get info only from one field and I actually need a string. So I changed the code a bit to
$currency_real = json_encode($query);
return $currency_real;
and my next output was
I believe that I miss something simple, but I can't find what (the output should be just "33.00"). Pardon me if the question is silly, I've started studying PHP not much time ago.
All best
just change this line
$out .= "<test>" . $currency_real->rows[0]->currency_real . "</test>";
or
$out .= "<test>" . $currency_real->row->currency_real . "</test>";
and don't use json_encode

graphaware get a returned count() value

So I have been working around with neo4j and php-client graph aware, until now i have make it work kind of ok. Until now that i try to run a query returning a count() and can't find how to catch the information, the query that i run is the next function:
function net_in_common($user, $other){
global $client;
$searchquery = "MATCH (a:user)-[r:IN_NET]->(b:user)<-[s:IN_NET]-(c:user) WHERE a.username = '" . $user . "' AND c.username = '" . $other . "' return count(DISTINCT b) as incommon";
$result = $client->run($searchquery);
return $result;
}
but when i try to echo it by
$common = net_in_common($user1, $user2);
echo $common->value('incommon');
i get absolute and completely nothing, it even dispatch an error that break the php code but i can't find the mistake itself.
it's a different way of fetching the value of a count() or something that i should do different??
The $result variable in your function returns you a Result object which itself contains a collection of ResultRecord objects (all is explained in the README of the client https://github.com/graphaware/neo4j-php-client#working-with-result-sets).
So, for reading the incommon value you would have to do :
$common = net_in_common($user1, $user2);
echo $common->firstRecord()->get('incommon');
Also, using php functions like this doesn't really reflect how we use php in (almost) 2017, maybe you can share a complete example of your project so we can investigate what's wrong, normally calling the value on a Result object should trigger an exception.

SQL + PHP Update query

I've been trying to update my data according to the user session (UserLogin) but it kept saying: Data type mismatch in criteria expression. The print_r is just for testing purposes.
Thanks in advance,
Z
function Employee1_BeforeShow(& $sender)
{
$Employee1_BeforeShow = true;
$Component = & $sender;
$Container = & CCGetParentContainer($sender);
global $Employee1; //Compatibility
$Page = CCGetParentPage($sender);
$db = $Page->Connections["PettyCashMDB"];
$sql1 = "UPDATE Employee SET Employee.LastActive = Date() WHERE Employee.[EmpID] = ". $_SESSION['UserLogin'];
$db->query($sql1);
print_r($_SESSION['UserLogin']);
$db->close();
Employee1_BeforeShow #67-67106FAD
return $Employee1_BeforeShow;
}
EDIT: I've tried #NanaPartykar 's method and by accident I've noticed that it does get the value from $_SESSION['UserLogin'], just that somehow the datatype is different.
EDIT: It displays the error Data type mismatch but both of them are string and returns string.
Instead of Employee.[EmpID], use Employee.EmpID
You need some quotes:
$sql1 = "UPDATE Employee SET Employee.LastActive = Date() WHERE Employee.[EmpID] = \'". $_SESSION['UserLogin'] . "\'";
Z - There are a bunch of built-in Codecharge functions to assist with getting values from querystring, sessions and controls.
eg: CCGetSession("UserLogin", "default");
http://docs.codecharge.com/studio50/html/index.html?http://docs.codecharge.com/studio50/html/Components/Functions/PHP/Overview.html
and executing SQL with some validating (from 'Execute Custom SQL' help topic):
$db = new clsDBConnection1();
$SQL = "INSERT INTO report (report_task_id,report_creator) ".
"VALUES (". $db->ToSQL(CCGetFromGet("task_id",0),ccsInteger) .",". $db->ToSQL(CCGetUserID(),ccsInteger) .")";
$db->query($SQL);
$db->close();
The $db->ToSQL (and CCToSQL) functions convert and add quotes for relevant data types (ccsText, ccsDate).
There are many examples in the Manual under 'Examples' and 'Programming Reference' for PHP (and ASP, .NET, etc)
http://support.codecharge.com/tutorials.asp
I strongly suggest looking at some of the examples, as Codecharge will handle a lot of the 'plumbing' and adding a lot of custom code will causing problems with the generation of code. In your example, you should add a 'Custom Code' action to the Record's 'Before Show' Event and add your code there. If you add code just anywhere, the entire section of code (eg: Before Show) will change colour and no longer be updated if you change something.
For example, if you manually edited the 'Update' function to change a default value, then no changes through the IDE/Properties will change the 'Update' function (such as adding a new field to the Record).
Finally got it to work, this is the code $sql1 = "UPDATE Employee SET LastActive = Date() WHERE EmpID = '$_SESSION[UserLogin]' "; Thanks to everyone that helped out.

Unreachable Statement in PHP

require_once 'C:/wamp/www/FirstWebsite/CommonFunctions.php';
function SelectRowByIncrementFunc(){
$dbhose = DB_Connect();
$SelectRowByIncrementQuery = "SELECT * FROM trialtable2 ORDER BY ID ASC LIMIT 1";
$result = mysqli_query($dbhose, $SelectRowByIncrementQuery);
$SelectedRow = mysqli_fetch_assoc($result);
return $SelectRowByIncrementQuery;
return $SelectedRow; //HERE is the error <-------------------------------
return $result;
}
$row = $SelectedRow;
echo $row;
if ($row['Id'] === max(mysqli_fetch_assoc($Id))){
$row['Id']=$row['Id'] === min(mysqli_fetch_assoc($Id));#TODO check === operator
}
else if($row['Id']=== min(mysqli_fetch_assoc($Id))){
$row['Id']=max(mysqli_fetch_assoc($Id));#TODO check === operator //This logic is important. DONT use = 1!
Ok, I am trying to write a program for the server end of my website using PHP. Using Netbeans as my IDE of choice I have encountered an error while attempting to write a function which will store a single row in an associative array.
The issue arises when I try to return the variable $SelectedRow. It causes an 'Unreachable Statment' warning. This results in the program falling flat on its face.
I can get this code to work without being contained in a function. However, I don't really feel that that is the way to go about solving my issues while I learn to write programs.
Side Notes:
This is the first question I have posted on SO, so constructive criticism and tips are much appreciated. I am happy to post any specifications that would help an answer or anything else of the sort.
I do not believe this is a so-called 'replica' question because I have failed to find another SO question addressing the same issue in PHP as of yet.
If anybody has any suggestions about my code, in general, I'd be stoked to hear, as I have only just started this whole CS thing.
You can only return one time. Everything after the first return is unreachable.
It's not entirely clear to me what you want to return from that function, but you can only return one value.
The return command cancels the rest of the function, as once you use it, it has served its purpose.
The key to this is to put all of your information in to an array and return it at the end of the function, that way you can access all of the information.
So try changing your code to this:
require_once 'C:/wamp/www/FirstWebsite/CommonFunctions.php';
function SelectRowByIncrementFunc(){
$dbhose = DB_Connect();
$SelectRowByIncrementQuery = "SELECT * FROM trialtable2 ORDER BY ID ASC LIMIT 1";
$result = mysqli_query($dbhose, $SelectRowByIncrementQuery);
$SelectedRow = mysqli_fetch_assoc($result);
$returnArray = array();
$returnArray["SelectRowByIncrementQuery"] = $SelectRowByIncrementQuery;
$returnArray["SelectedRow"] = $SelectedRow;
$returnArray["result"] = $result;
return $returnArray;
}
And then you can access the information like so:
$selectedArray = SelectRowByIncrementFunc();
$row = $selectedArray["SelectedRow"]
And so forth...

Update query in mysqli function using object oriented php

I am doing work with php object oriented and mysqli. i have class name Database.php inside this class i have defined a function name update.
i did some thing wrong i think that's why it doesn't work fine. when i use to run click at update then data retrieves in Form but when i try to save the updated form it always updated only first column of my database. function code is given below:
public function update($tablename, $value, $where){
$Update = " UPDATE ".$tablename." SET ";
$array_keys = array_keys($value);
$array_values= array_values($value);
$count = count($array_keys);
for($i=0; $i< $count; $i++){
$value[$i] = " = '".$value[$array_keys[$i]]."' ";
}
//$value = implode(" ",$value);
print_r($value);
$Update .= " ".$value." WHERE ".$where;
$Utest = $this->DbCon->query($Update);
if($Utest){
return true;
}else{
return false;
}
here is php code that i have tried before
if(isset($_POST['update'])){
$id1 = $_POST['id'];
$name = $_POST['name'];
$city = $_POST['city'];
$success=$DbQuery->update("record", array(" name " => $name ,"city" => $city)," id =".$id1);
if($success){
header("LOCATION:index.php");
}else{
echo "try again ";
}
}
do you have any good suggestion for update function how ic an improve this function. actually i am new in php object oriented so suggest me easy method to this. Thanks
The problem is that you're trying to convert the $value array to a string in $Update .= " ".$value." WHERE ".$where;. You can't do that. You have to construct the query in the for loop above it instead of populating the $value array. Either way you shouldn't be building the query by hand. You REALLY should use PDO::prepare() or $mysqli::prepare() to prevent vulnerabilities. Yes, that might feel like an overkill if you're just learning, but you should start forming good habits from the very beginning.
Also, fix your formatting. Your code is really hard to read. I recommend reading through the PSR standard (or any widespread PHP standard) and adapting it in your code. Again, might feel like overkill, but it's a good habit to pick up early.
Here's an adapted version of your code for you to easily play with. You can see the problem if you run php index.php in the CLI window at the bottom.

Categories