PDO::FETCH_ASSOC Does not return the correct table columns - pdo

I'm new to PHP and I am currently developing a small web application. Below is the test code for the DB query using PDO:
try
{
$dsn = "mysql:host=localhost;dbname=auction";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"
);
$pdo = new PDO($dsn, 'root', 'password', $opt);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$stmt = $pdo->query("SELECT id,username,password,name,level FROM users");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$test = $stmt->fetchALL();
print_r($test);
The output of the said code is the following:
Array ( [0] => Array ( [id] => 1 [username] => admin [users] => Administrator [auction] => 0 )
[1] => Array ( [id] => 2 [username] => jodel [users] => Jodel Ross [auction] => 1 ) )
I am confused because, from my point of view, the above code should have given an associative array whose index are the columns of the returned result set, i.e. id, username,password,name,level. But it is not so.
Moreover, If I used PDO::FETCH_NUM, the correct number of fields and corresponding data are returned correctly as show below:
Array ( [0] => Array ( [0] => 1
[1] => admin
[2] => 21232f297a57a5a743894a0e4a801fc3
[3] => Administrator
[4] => 0 )
[1] => Array ( [0] => 2
[1] => jodel_ross
[2] => 2cdaeb5df4cf941d9c5650591cba1fdc
[3] => Jodel Ross, Jr.
[4] => 1 ) )
Please help, I need to understand why and I have search the web for answers and have not found any.
Thanks.
Development environment:
Windows 7 x64
PHP 5.3.6
Mysql 5.5
Apache 2.0.64
Zend Framework 1.11

There is still an open error with Zend Framework 1.x, PHP 5.3.x and setFetchMode() and that's the reason why it doesn't work as expected.
see http://framework.zend.com/issues/browse/ZF-10866 and http://framework.zend.com/issues/browse/ZF-3470
Please vote for closing this error or submit a patch.

Related

ProcessMaker get data from SqlServer returned error

I am going to bind a dropdown from Sql Server database in ProcessMaker DynaForm. I set up the connection and write the query. My query return data which some of them are in Persian language, so the dropdown failed to bind and Chrome Inspector shows the error:
Unexpected token ;
Whenever I changed my query in which the returned data is not in Perisan language, every thing works fine!
Even when I write a mock query such as: Select 1, N'وحید', whitout referencing any table, every thing works fine too!
How can I confugre processmaker to show the Persian language data in dropdown too?
UPDATE
I found the php class which executes the query and get the returned data. Here is the result which is returned:
Array ( [0] => 1 [1] => ��� ����� )
Array ( [0] => 2 [1] => ��� �Ә��� )
Array ( [0] => 3 [1] => ��� ������� )
Array ( [0] => 4 [1] => ������ )
Array ( [0] => 5 [1] => ��� ͘�� )
I think there is some problem with connection encoding, but I do not know how to resolve it!
Here is my connection to sql server's details:
MSSQLStatement Object ( [conn:protected] => MSSQLConnection Object ( [database:MSSQLConnection:private] => MyDB [transactionOpcount:protected] => 0 [dblink:protected] => Resource id #18 [dsn:protected] => Array ( [database] => MyDB [encoding] => utf8 [hostspec] => MyInstanceName [password] => MyPassword [phptype] => mssql [port] => [protocol] => [socket] => [username] => MyUserName ) [flags:protected] => 0 [lastQuery] => select Id, Title From MyTable ) [resultSet:protected] => [updateCount:protected] => [warnings:protected] => Array ( ) [resultClass:protected] => [stmt:protected] => [limit:protected] => 0 [offset:protected] => 0 )
Are you adding a semicolon at the end of the query?
If you checkout the ProcessMaker documentation, they indicate that when using SQL server, semicolon should not be added:

Telegram photo format with getUserPhotos

I use getUserPhotos to get profile photos. What is the main standard source that I should use for html src attribute? (I searched even in telegram's docs, but didn't get the solution.)
A part of my result (type is photos.Photos):
Array
(
[_] => photos.photosSlice
[count] => 14
[photos] => Array
(
[0] => Array
(
[_] => photo
[has_stickers] =>
[id] => 195744071274310414
[access_hash] => -5116812755800708610
[date] => 1472714208
[sizes] => Array
(
[0] => Array
(
[_] => photoSize
[type] => a
[location] => Array
(
[_] => fileLocation
[dc_id] => 4
[volume_id] => 425426808
[local_id] => 100105
[secret] => -7781982930425156181
)
[w] => 160
[h] => 160
[size] => 11694
)
...
What you need to do is use the following method to download your file or picture:
upload.getFile#e3a6cfb5 location:InputFileLocation offset:int limit:int = upload.File;
Where InputFileLocation is defined below, and may be obtained from PhotoSize.location in your question above:
inputFileLocation#14637196 volume_id:long local_id:int secret:long = InputFileLocation;
more details on file download here: https://core.telegram.org/api/files#downloading-files

facebook Graph api fetch list of all friends

I am working in a project where i have to integrate face-book graph-api. Everything is going well
but i stuck in a problem to fetch my face book friend list using /me/friends/. It return me only
those friends which has log in with my app, but i want my all friend list
$session = new FacebookSession( $session->getToken());
$friends= (new FacebookRequest( $session, 'GET', '/me/friends' ))->execute()->getGraphObject()
->asArray();
echo '<pre>' . print_r( $friends, 1 ) . '</pre>';
It return
[data] => Array
(
[0] => stdClass Object
(
[name] => Nitin Chamola
[id] => 792718557454395
)
[1] => stdClass Object
(
[name] => Ajay Aggarwal
[id] => 765446336856080
)
[2] => stdClass Object
(
[name] => Manjit Singh Jassal
[id] => 1538019986415496
)
[3] => stdClass Object
(
[name] => Sapna Thakur
[id] => 1518184435101741
)
)
[paging] => stdClass Object
(
[next] => https://graph.facebook.com/v2.2/1587893521432407/friends?access_token=CAAGPXznoOFkBAOCKznoJ4AwZBi9DhlNQYuvgn1qT1Qz3x0JE5mUT5JXOnEnhPtpcriJjoRB5K2yNW3B2L0PtJEcgy8MK3NbZB2oHeBPEeayudFSSxDDOTYUc4J3Q9zDkBqsIGcDcHyzECBtHqOM2HKRUwdOmsSRAJQ41lVWj0T1O2bH1P4ZC2Or2Gv0FuVTL7Wp2xudgGsDHyjwLvGT&limit=25&offset=25&__after_id=enc_AexVs_nuUmTOuF4B5iplPfzsuPs6vtQGx7TV_pH-cINIX2nUIvn3vdmWzNINrKx-vqDnpavxrIA_kr3aqSr6lw4k
)
[summary] => stdClass Object
(
[total_count] => 242
)
After Graph API 2 and above you can no longer do this. You can only fetch the list of those friends who use your app i,e who have given permission to your app. This has been done by fb for security reasons.

how to get result for api connecting to whoapi?

As according to the mention documentation at whoapi.com i have gone through as mention according to instruction and tried implementing the api but the variable doesn't pass from one page to other.For example i have one page which have input type and submit button then when i click submit button it should redirect to who api url and display result
http://api.whoapi.com/?domain=whoapi.com&apikey=demokey&r=blacklist
whoapi.com = name of url,demokey = key assigned by who api & blacklist = name of service of who api
guide me through the connecting protocol.
Not sure I understand the question. Using the API should be straight simple as any REST request is.
PHP example:
$domain = "whoapi.com";
$r = "blacklist";
$apikey = "demokey";
$request = "http://api.whoapi.com/?domain=$domain&r=$r&apikey=$apikey";
$output = json_decode(file_get_contents($request), true);
Resulting array with print_r($output) looks like this:
[status] => 0
[ip] => 88.198.98.181
[blacklisted] => 0
[blacklists] => Array
(
[0] => Array
(
[tracker] => invaluement.com
[blacklisted] => 0
)
[1] => Array
(
[tracker] => surbl.org
[blacklisted] => 0
)
[2] => Array
(
[tracker] => barracudacentral.org
[blacklisted] => 0
)
[3] => Array
(
[tracker] => sorbs.net
[blacklisted] => 0
)
[4] => Array
(
[tracker] => spamhaus.org
[blacklisted] => 0
)
)
Hope this helps.

Magento Cart API not showing prices

I am attempting to use the Magento Enterprise 1.10 XML-RPC API to handle cart/catalog functions outside of the Magento installation. The issue that I am having is when I add to cart. I can connect just fine to the API endpoint, login, and retrieve data. The following is the code that I am using to discover the workings of the Magento API.
<?php
require $_SERVER['DOCUMENT_ROOT'].'/Zend/XmlRpc/Client.php';
$url = 'http://mymagento.com/api/xmlrpc';
$user = 'apiuser';
$pass = 'apipass';
$proxy = new Zend_XmlRpc_Client( $url );
$sess = $proxy->call( 'login', array( $user, $pass ) );
$cartId = $proxy->call( 'call', array( $sess, 'cart.create', array( 1 ) ) );
$pList = $proxy->call( 'call', array( $sess, 'product.list', array() ) );
$cList = $proxy->call( 'call', array( $sess, 'customer.list', array() ) );
$cList[0]['mode'] = 'customer';
$setCart = $proxy->call( 'call', array( $sess,
'cart_customer.set',
array( $cartId, $cList[0] ) ) );
foreach( $pList as $prod)
{
if( $prod['product_id'] == 5 )
{
$prod['qty'] = 5;
$addCart = $proxy->call( 'call', array( $sess,
'cart_product.add',
array( $cartId, $pAdd ) ) );
}
}
$cList = $proxy->call( 'call', array( $sess, 'cart.info', array( $cartId ) ) );
print_r( $cList );
Outputs:
[store_id] => 1
[created_at] => 2011-05-27 13:30:57
[updated_at] => 2011-05-27 13:31:00
[converted_at] => 0000-00-00 00:00:00
[is_active] => 0
[is_virtual] => 0
[is_multi_shipping] => 0
[items_count] => 1
[items_qty] => 5.0000
[orig_order_id] => 0
[store_to_base_rate] => 1.0000
[store_to_quote_rate] => 1.0000
[base_currency_code] => USD
[store_currency_code] => USD
[quote_currency_code] => USD
[grand_total] => 0.0000
[base_grand_total] => 0.0000
[checkout_method] => customer
...
[items] => Array
(
[0] => Array
(
[item_id] => 93
[quote_id] => 119
[created_at] => 2011-05-27 13:31:00
[updated_at] => 2011-05-27 13:31:00
[product_id] => 5
[store_id] => 1
[parent_item_id] =>
[is_virtual] => 1
[sku] => product1
[name] => product
[description] =>
[applied_rule_ids] =>
[additional_data] =>
[free_shipping] => 0
[is_qty_decimal] => 0
[no_discount] => 0
[weight] =>
[qty] => 5
[price] => 0.0000
[base_price] => 0.0000
[custom_price] =>
[discount_percent] => 0.0000
[discount_amount] => 0.0000
[base_discount_amount] => 0.0000
However, I am to just call the following using the same above session
<?php
$pInfo = $proxy->call( 'call', array( $sess, 'catalog_product.info', '5' ) );
print_r( $pInfo );
I get the following information about the product:
[product_id] => 5
[sku] => product1
[set] => 9
[type] => virtual
[categories] => Array
(
)
[websites] => Array
(
[0] => 1
)
[type_id] => virtual
[name] => product
[description] => Test
[short_description] => Test
[news_from_date] =>
[old_id] =>
[news_to_date] =>
[status] => 1
[visibility] => 4
...
[created_at] => 2011-05-25 15:11:34
[updated_at] => 2011-05-25 15:11:34
...
[price] => 10.0000
In the end, the API sees that the price of the item is in fact $10.00, but when added to the cart via API the prices are not properly reflected.
Just so it can be an officially answered question, here is the solution found, http://magentocommerce.com/boards/viewthread/227044 I spent two days searching for this, and today come up with an obscure search term to try and find the solution
I've been looking at this issue for a couple days now. It didn't make sense to me that if you add a product to your cart through the normal web interface [ie Mage_Checkout_CartController::addAction() ]. It knows the price without you providing an address. I finally found the difference between the two. In addAction() they create an instance of Mage_Checkout_Model_Cart, add the product to that, and save it. In the api they use Mage_Sales_Model_Quote instead. If you look at Mage_Checkout_Model_Cart::save() you will see these two lines:
$this->getQuote()->getBillingAddress();
$this->getQuote()->getShippingAddress();
These two lines actually create empty Mage_Sales_Model_Quote_Address objects that get saved into the db.
If you are willing/able to modify magento's code, you can modify Mage_Checkout_Model_Cart_Api::create() and add a call to these two methods before $quote->save() and the api and the web interface will work the same way.
I have only tested this a little bit, but I really think this is a bug and not a feature. I'll see about getting this in front of actual magento devs and maybe they'll include it in the next release.
Magento frontend and API both are different. In frontend after registering customer it creates quote for customer and also set address with that created quote id. But in API it creates only quote using shoppingCartCreate service. For proper we need to customize create service. I did and it worked for me.
Here am providing solution:
Edit function in file - Mage/Checkout/Model/Cart/Api.php
public function create($store = null)
{
$storeId = $this->_getStoreId($store);
try {
/*#var $quote Mage_Sales_Model_Quote*/
$quote = Mage::getModel('sales/quote');
$quote->setStoreId($storeId)
->setIsActive(false)
->setIsMultiShipping(false)
->save();
/* Customized this for saving default address for quote and it will show price in cart info*/
$quote->getBillingAddress();
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->collectTotals();
$quote->save();
/* End cart here */
} catch (Mage_Core_Exception $e) {
$this->_fault('create_quote_fault', $e->getMessage());
}
return (int) $quote->getId();
}