PHP - Azure Table Storage in with more than 1000 entities - php

I am trying to query from my table storage by using the Azure SDK for PHP.
My query looks like:
$tableRestProxy = ServicesBuilder::getInstance()->createTableService($this->connectionString);
$filter = "( PartitionKey eq '$id' )";
$options = new QueryEntitiesOptions();
$options->setFilter(Filter::applyQueryString($filter));
$result = $tableRestProxy->queryEntities('test', $options);
$entities = $result->getEntities();
$nextPartitionKey = $result->getNextPartitionKey();
$nextRowKey = $result->getNextRowKey();
while (!is_null($nextRowKey) && !is_null($nextPartitionKey) ) {
$options = new QueryEntitiesOptions();
$options->setNextPartitionKey($nextPartitionKey);
$options->setNextRowKey($nextRowKey);
$options->setFilter(Filter::applyQueryString($filter));
$result2 = $tableRestProxy->queryEntities("test", $options);
$newentities = $result2->getEntities();
$entities=array_merge($newentities, $entities);
}
The issue: When running into the while loop I always get the first 1000 back entities, with the same nextrowkey and nextpartitionkey for each query. Thus it creates an infinit loop.
What am I getting wrong with the continuation of a query?
Any help is appreciated.

#Gaurav: this is true, but occurs only on the second loop (I forgot to add the two lines when posting my code).
I've been trying to find out what's wrong for at least half a day. Finally I got it:
It is due to an older version of the Windows Azure PHP SDK which has a "bug". I stumbled across this "bug" at the bottom of this thread: https://github.com/Azure/azure-sdk-for-php/issues/702
The older version of Windows Azure SDK uses _encodeODataUriValue which seems to be unnecessary.

Related

How do you combine ParseQuery::orQueries with matchesQuery using the php parse-server sdk

I'm moving away from the REST API in my PHP code and converting to the php sdk for parse.
I am having trouble converting this REST API query to the proper syntax for the parse php-sdk and could use a few pointers.
This is the working REST API query.
where=
{"phostId":
{"__type":"Pointer","className":"Hosts","objectId":"'.$hostObjId.'"},
"isCompany":false,
"expunged":{"$nin":[true]},
"$or":[
{"endDate":
{"$gte":
{
"__type":"Date",
"iso":"'.$now.'"
}
}
},
{"isPerm":true}
]
}
&keys=pvisitorId,company,isPerm,startDate,endDate,name
&include=pvisitorId&order=-name';
I can query based on the Pointer with no issue but I am not able to figure out how to work in the OR clause.
This is what I have so far.
//Query the pointer for an object id matching our user session id
$innerQuery = new ParseQuery("Hosts");
$innerQuery->equalTo("objectId",$_SESSION['host_object_id'] );
//Building two queries used for the OR condition
$endDate = new ParseQuery("Authorizations");
$endDate->greaterThan("endDate", $date);
$isPerm = new ParseQuery("Authorizations");
$isPerm->equalTo("isPerm", True);
//create primary query
$query = new ParseQuery("Authorizations");
//set filters
$query->equalTo("isCompany",False);
$query->notEqualTo("expunged",True);
////This is what I am trying to add to $query just not sure how to do it.
$mainQuery = ParseQuery::orQueries([$endDate, $isPerm]);
$results1 = $mainQuery->find();
//Sort, Limit, add InnerQuery
$query->addDescending("name");
$query->limit(1);
$query->matchesQuery("phostId", $innerQuery);
// All results:
$results = $query->find();
Thanks in advance for any help or pointers on what I am missing.
You can only use matchesQuery for pointers. For your case, you have to use matchesKeyInQuery.
$query->matchesKeyInQuery("phostId", "objectId", $innerQuery);
Replace objectId with column name of ID that you want to compare with.
Hope this helps :)

how to retrive more than 1000 rows in azure tables php

I am trying to get more than 1000 rows from azure in php.
First of all i am not able to use filter class. which namespace need to be added to use filter class
after that while loop is gng in infinite loop
any help
$tableRestProxy = ServicesBuilder::getInstance()->createTableService($this->connectionString);
$filter = "( PartitionKey eq '$id' )";
$options = new QueryEntitiesOptions();
$options->setFilter(Filter::applyQueryString($filter));
$result = $tableRestProxy->queryEntities('test', $options);
$entities = $result->getEntities();
$nextPartitionKey = $result->getNextPartitionKey();
$nextRowKey = $result->getNextRowKey();
while (!is_null($nextRowKey) && !is_null($nextPartitionKey) ) {
$options = new QueryEntitiesOptions();
$options->setNextPartitionKey($nextPartitionKey);
$options->setNextRowKey($nextRowKey);
$options->setFilter(Filter::applyQueryString($filter));
$result2 = $tableRestProxy->queryEntities("test", $options);
$newentities = $result2->getEntities();
$entities=array_merge($newentities, $entities);
}
link m using is
PHP - Azure Table Storage in with more than 1000 entities
You can leverage setTop() function of MicrosoftAzure\Storage\Table\Models\QueryEntitiesOptions class to select the top X (number) entities of the table.
And according the description at https://github.com/Azure/azure-storage-php/blob/master/src/Table/Models/QueryEntitiesOptions.php#L148, we can find that the filter classes have moved into the namespace MicrosoftAzure\Storage\Table\Models\Filters
If you want to use the filter classes in the new Azure Storage SDK for PHP, you can try to include the package as:
use MicrosoftAzure\Storage\Table\Models\Filters\QueryStringFilter;
Please consider the following code snippet:
use MicrosoftAzure\Storage\Table\Models\QueryEntitiesOptions;
use MicrosoftAzure\Storage\Table\Models\Filters\QueryStringFilter;
$options = new QueryEntitiesOptions();
$filter = new QueryStringFilter("(RowKey eq '".$id."')");
$options->setFilter($filter);
$options->setTop(1000);

Website Version PHP

I am trying to create a version file that will alert the end user if there is an update.
In theory and practical application:
When the admin logs in and goes to their Management interface, a script reads a remote file and compares the remote version with the local version, if they are the same, report that status, if remote is greater, alert.
The local version is pulled from a MySQL table with MySQLi and confirmed to work, the remote file is read, confirmed to work as well how ever I am running into an issue.
Local version is 1.3, remote version is 1.3 yet it reports:
Your Version - 1.3 is OUT OF DATE, new version 1.3 is available for download
When it should say
Your Version is UP TO DATE!
The code I am using is below
require_once('includes/settings.php');
function check_version() {
global $connection;
$query = "SELECT * from version";
$result = mysqli_query($connection, $query);
// if the query fails, die and report
if(!$result = $connection->query($query)){
die('There was an error running the query [' . $connection->error . ']');
}
else {
$row = mysqli_fetch_assoc($result);
$my_version = $row['minor'];
echo '<p>Local = '.$my_version.'</p>';
$ctx = stream_context_create(array(
'http' => array(
'timeout' => 1
)
));
$rversion = file_get_contents("http://www.twedev.com/projects/microcms/version", 0, $ctx);
echo $rversion;
var_dump($rversion);
if ($my_version === $rversion) {
$dev_msg = '<strong>Your Micro CMS Version - '.$my_version.' is CURRENT</strong>';
}
elseif ($my_version != $rversion) {
$dev_msg = '<strong>Your Micro CMS Version - '.$my_version.' is OUT OF DATE,<br> new version '.$rversion.' is available for download</strong>';
}
else {
$dev_msg = '<strong>Something went wrong with the version server</strong>';
}
}
return $dev_msg;
}
?>
What I need is another set of eyes to look through this is point me in the right direction if possible, this has me overly frustrated today.
You are using the === identical operator to compare values, using that instead of == will compare the value and the type of value.
The value you are getting from file_get_contents() it's a string, and the version number seems to be something different to string, probably a float number.
Update: After using var_dump on both vars we could see the result wasn't the same, and the solution was to use trim to strip whitespaces from the end of the string.

PHP loading XML stream causes MySQL INSERT INTO table to no longer work

I've been working on this single problem all day and I can not make light of it, so thought I'd ask the seasoned professionals!
Essentially, if I remove the XML connecting lines (first four at the top), my "INSERT INTO" statements work (using static values), if I don't remove them I get no error message from MySQL / PHP.... they simply do not work.
The database is all set to accept nulls (temporarily) on all fields, and they're all VARCHAR for now... just until I can figure out what is going on.
The XML feed seems to connect without a problem, as the page takes ages to load and when I ECHO the output, all the correct values are coming in.
The biggest thing to note is that when I echo the INSERT INTO command, then copy and paste it into PHP My Admin... the actual insert line works perfectly fine... just when the XML is turned on; it doesn't work.
Does anyone know why I can not seem to use:
$result = mysqli_query($cxn, $query);
After an XML feed has been connected to?
$context = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
$url = "https://thedatastream.xml";
$xml = file_get_contents($url, false, $context);
$xml = simplexml_load_string($xml);
foreach ($xml->row as $item) {
$partNumber = strval(escape($item->PartNumber));
$partName = strval(escape($item->Name));
$partBrand = strval(escape($item->Manufacturer));
$partCategory = strval(escape($item->CategoryName));
$partGroup = strval(escape($item->Group));
$partQty = intval(escape($item->Quantity));
$partCostEx = floatval(escape($item->PriceCostEx));
$partRetailEx = floatval(escape($item->PriceRetailEx));
$partDesc = strval(escape($item->Description));
$partHTMLDesc = strval(escape($item->HTMLDescription));
$partImg = strval($item->image_large);
$partPDF = strval($item->PDFURL);
$partUpdated = strval(escape($item->StockRecordUpdated));
$partETA = strval($item->ETADate);
$partETAStatus = strval($item->ETAStatus);
$query15 = "INSERT INTO `downloadTbl` (`partNumber`, `partName`, `Manufacturer`, `CategoryName`, `Group`, `Unit`, `PriceCostEx`, `PriceRetailEx`, `Description`, `HTMLDescription`, `image_large`, `PDFURL`, `StockRecordUpdated`, `ETADate`, `ETAStatus`) VALUES ('".$partNumber."', '".$partName."', '".$partBrand."', '".$partCategory."', '".$partGroup."', '".$partQty."', '".$partCostEx."', '".$partRetailEx."', '".$partDesc."', '".$partHTMLDesc."', '".$partImg."', '".$partPDF."', '".$partUpdated."', '".$partETA."', '".$partETAStatus."')";
$result15 = mysqli_query($cxn, $query15);
}
I have the answer!
The downloading of the XML file was causing a time out of the database connection,
I simply created a second connection after the XML file completed downloading and used that for the INSERT statements.
$cxn2 = mysqli_connect($host, $user, $password, $db);
I think you could just re-connect the first database connection, instead of making a second one.
Thank you to everyone who helped to resolve this problem.

Get value from Cassandra with PHPCASSA

I recently switched to PHPCassa to manage db connection in my PHP platform.
This is the code i'm using:
$indexExpression = new IndexExpression("Username", $username);
$indexClause = new IndexClause(array($indexExpression));
$cf = new ColumnFamily($this->cassandra, "Users");
$rows = $cf->get_indexed_slices($indexClause);
The problem is that actually $rows is not an array containing the data i'd like to fetch but it contains an IndexedColumnFamilyIterator object.
I'm I doing something wrong?
Thanks for helping.
Since you already cross-posted to the user mailing list (tisk, tisk :), I'll link to the answer and copy the answer here for others: https://groups.google.com/forum/?fromgroups#!topic/phpcassa/RrYTQc_jQ7s
It returns an iterator so that it can break up the query into manageable chunks (100 rows, by default) automatically.
$row_iterator = $cf->get_indexed_slices($indexClause);
foreach ($row_iterator as $key => $columns) {
// do stuff
}

Categories