How to pass variable to query in php?
Date: March 21, 2012 02:12AM
class Sample_Event_Model_Observer
{
public function Mytestmethod($observer)
{
$event = $observer->getEvent();
$incrementid = $observer->getEvent()->getOrder()->getIncrementId(); global $con;
$db_name = "magento";
$con = mysql_connect("localhost", "magento", "password");
If (!$con) {
die('Could not connect: ' . mysql_error());
mysql_close($con); }
$seldb = mysql_select_db($db_name, $con);
If ($seldb == $db_name) {
$query = "SELECT sfop.`po_number`,sfo.`created_at`,sfo.`customer_firstname` , sfo.`customer_lastname` , sfo.`customer_email` , sfo.`shipping_description` , sfoa. firstname, sfoa. lastname, sfoa. company, sfoa. street, sfoa. city, sfoa. region, sfoa.country_id, sfoa. postcode, sfoa. telephone, sfoa1. firstname,sfoa1. lastname, sfoa1. company, sfoa1. street, sfoa1. city, sfoa1. region, sfoa1.country_id, sfoa1. postcode, sfoa1. telephone, cg. customer_group_code, cg1. customer_group_code, cev. value, cev1. value, cev2. value, cev3. value, sfo.`increment_id`
FROM `sales_flat_order_payment` sfop, `sales_flat_order` sfo, `sales_flat_order_address` sfoa, `sales_flat_order_address` sfoa1, `customer_entity` ce, `customer_entity` ce1, `customer_group` cg, `customer_group` cg1, `customer_entity_varchar` cev, `customer_entity_varchar` cev1, `customer_entity_varchar` cev2, `customer_entity_varchar` cev3
WHERE sfo.`entity_id` = sfop.`parent_id` AND sfo.`shipping_address_id` = sfoa.`entity_id` AND sfo.`billing_address_id` = sfoa1.`entity_id` AND sfo. ge_dealer_id = ce. entity_id AND ce. group_id = cg. customer_group_id AND sfo. ge_customer_id = ce1. entity_id AND ce1. group_id = cg1. customer_group_id AND sfo. ge_dealer_id = cev.entity_id AND cev. attribute_id = 5 AND sfo. ge_dealer_id = cev1.entity_id AND cev1. attribute_id = 7 AND sfo. ge_customer_id = cev2.entity_id AND cev2. attribute_id = 5 AND sfo. ge_customer_id = cev3.entity_id AND cev3. attribute_id = 7 AND sfo.`increment_id` ='".$incrementid."';" ;
$result = mysql_query($query);
if (!$result)
{
$str='Could not run query: ' . mysql_error();
}else{
$str1="ok number of rows".mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
$str = "'". $row["po_number"] . "',". "'" . $row["created_at"] . "'," . "'" . $row["customer_firstname"] . "',". "'" . $row["customer_lastname"] . "'," . "'" . $row["customer_email"] . "'," . "'" . $row["shipping_description"]. "'," . "'" . $row["firstname"] . "'," . "'" . $row["lastname"] ."'," . "'" . $row["company"] . "'," . "'" . $row["street"] ."'," ."'" . $row["ship_to_city"] . "'," ."'" . $row["region"] . "'," ."'" . $row["country_id"] ."'," ."'" . $row["postcode"]."'," ."'" . $row["telephone"] . "'," ."'" . $row["firstname"] . "'," ."'" . $row["lastname"] ."'," ."'" . $row["company"]."'," ."'" . $row["street"] . "'," ."'" . $row["city"] ."'," ."'" . $row["region"] . "'," ."'" . $row["country_id"] ."'," ."'" . $row["postcode"] . "'," ."'" . $row["telephone"] . "'," ."'" . $row["customer_group_code"] ."'," ."'" . $row["customer_group_code"]."'," ."'" . $row["value"] . "'," ."'" . $row["value"] ."'," ."'" . $row["value"] . "'," ."'" . $row["value"] ."'," ."'" . $row["increment_id"];
}
}
}
$eventmsg = "Current Event Triggered : <I>" . $event->getName() . "</I>". "<br />"."$incrementid" ."<br />".$str."<br />".$query."<br />".$str1;
echo Mage::getSingleton('checkout/session')->addSuccess($eventmsg);
}
}
?>
o/p is-:
Current Event Triggered : sales_order_place_after
100000306
SELECT sfop.`po_number`,sfo.`created_at`,sfo.`customer_firstname` , sfo.`customer_lastname` , sfo.`customer_email` , sfo.`shipping_description` , sfoa. firstname, sfoa. lastname, sfoa. company, sfoa. street, sfoa. city, sfoa. region, sfoa.country_id, sfoa. postcode, sfoa. telephone, sfoa1. firstname, sfoa1. lastname, sfoa1. company, sfoa1. street, sfoa1. city, sfoa1. region, sfoa1.country_id, sfoa1. postcode, sfoa1. telephone, cg. customer_group_code, cg1. customer_group_code, cev. value, cev1. value, cev2. value, cev3. value, sfo.`increment_id` FROM `sales_flat_order_payment` sfop, `sales_flat_order` sfo, `sales_flat_order_address` sfoa, `sales_flat_order_address` sfoa1, `customer_entity` ce, `customer_entity` ce1, `customer_group` cg, `customer_group` cg1, `customer_entity_varchar` cev, `customer_entity_varchar` cev1, `customer_entity_varchar` cev2, `customer_entity_varchar` cev3 WHERE sfo.`entity_id` = sfop.`parent_id` AND sfo.`shipping_address_id` = sfoa.`entity_id` AND sfo.`billing_address_id` = sfoa1.`entity_id` AND sfo. ge_dealer_id = ce. entity_id AND ce. group_id = cg. customer_group_id AND sfo. ge_customer_id = ce1. entity_id AND ce1. group_id = cg1. customer_group_id AND sfo. ge_dealer_id = cev.entity_id AND cev. attribute_id = 5 AND sfo. ge_dealer_id = cev1.entity_id AND cev1. attribute_id = 7 AND sfo. ge_customer_id = cev2.entity_id AND cev2. attribute_id = 5 AND sfo. ge_customer_id = cev3.entity_id AND cev3. attribute_id = 7 AND sfo.`increment_id` = '100000306';
ok number of rows0
This is my code. $incrementid is the variable I want to pass to query at the end in where condition. Once in my application I press Place order tab, $incrementid get generated which I need to pass to mysql query. When I echo query, it's showing increment id value but unable to fetch any result. Also mysql_num_rows always shows 0. Plz guide me...I just want values in $str i.e. my query must fetch all values.
Edited 2 time(s). Last edit at 03/21/2012 02:17AM by pratik kanawade.