I'm using this PHP code to get objects from class.
I've got 100000 objects.
I want to get all the objects in a single query.
I'm using the following code.
$query = new ParseQuery("news_master");
$results = $query->find();
You can remove limit to get all data, please try following code :-
$query = new ParseQuery("news_master");
$query->equalTo("All",true);
results = $query->find();
As of 2018 and Parse Community PHP SDK and server you can use the each function, which provides a callback and can iterate over all data. Note this cannot be used in conjunction with skip, sort, limit. See Docs
An example of a query from their TEST suite would look like this.
$query = new ParseQuery('Object');
$query->lessThanOrEqualTo('x', $count);
$values = [];
$query->each(
function ($obj) use (&$values) {
$values[] = $obj->get('x');
},
10
);
$valuesLength = count($values);
the value of 10 is what batch size you want. If your database table is locked down and requires master key then you can do the following.
$query = new ParseQuery('Object');
$query->lessThanOrEqualTo('x', $count);
$values = [];
$query->each(
function ($obj) use (&$values) {
$values[] = $obj->get('x');
},
true, 10 // notice the value of true
);
$valuesLength = count($values);
The reason I'm adding to this old comment is because if you search getting more than 1000 records from parse no good link comes up and this is usually the first one.
Cheers to anyone that stumbles across this!
Related
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 :)
In Zend app, I use Zend\Db\TableGateway and Zend\Db\Sql to retrieve data data from MySQL database as below.
Model -
public function getCandidateEduQualifications($id)
{
$id = (int) $id;
$rowset = $this->tableGateway->select(function (Sql\Select $select) use ($id)
{
$select->where
->AND->NEST->equalTo('candidate_id', $id)
->AND->equalTo('qualification_category', 'Educational');
});
return $rowset;
}
View -
I just iterate $rowset and echo in view. But it gives error when try to echo two or more times. Single iteration works.
This result is a forward only result set, calling rewind() after
moving forward is not supported
I can solve it by loading it to another array in view. But is it the best way ? Is there any other way to handle this ?
$records = array();
foreach ($edu_qualifications as $result) {
$records[] = $result;
}
EDIT -
$resultSet->buffer(); solved the problem.
You receive this Exception because this is expected behavior. Zend uses PDO to obtain its Zend\Db\ResultSet\Resultset which is returned by Zend\Db\TableGateway\TableGateway. PDO result sets use a forward-only cursor by default, meaning you can only loop through the set once.
For more information about cursors check Wikipedia and this article.
As the Zend\Db\ResultSet\Resultset implements the PHP Iterator you can extract an array of the set using the Zend\Db\ResultSet\Resultset:toArray() method or using the iterator_to_array() function. Do be careful though about using this function on potentially large datasets! One of the best things about cursors is precisely that they avoid bringing in everything in one go, in case the data set is too large, so there are times when you won't want to put it all into an array at once.
Sure, It looks like when we use Mysql and want to iterate $resultSet, this error will happen, b/c Mysqli only does
forward-moving result sets (Refer to this post: ZF2 DB Result position forwarded?)
I came across this problem too. But when add following line, it solved:
$resultSet->buffer();
but in this mentioned post, it suggest use following line. I just wonder why, and what's difference of them:
$resultSet->getDataSource()->buffer();
This worked for me.
public function fetchAll()
{
$select = $this->tableGateway->getSql()->select();
$resultSet = $this->tableGateway->selectWith($select);
$resultSet->buffer();
$resultSet->next();
return $resultSet;
}
$sql = new Zend\Db\Sql($your_adapter);
$select = $sql->select('your_table_name');
$statement = $sql->prepareStatementForSqlObject($select);
$results = $statement->execute();
$resultSet = new ResultSet();
$resultSet->initialize($results);
$result = $resultSet->toArray();
Near the top of a PHP page I have a mySQL query followed by a do-while loop.
$query_offer = "SELECT offer, offer_text FROM ad_offers WHERE hid LIKE '$hid' AND show_from < CURRENT_DATE() AND show_to > CURRENT_DATE()";
$offer = mysql_query($query_offer, $MySQL_extranet) or die(mysql_error());
$row_offer = mysql_fetch_assoc($offer);
do {
SOME PHP STUFF
}while($row_offer = mysql_fetch_assoc($offer));
Then further down the page I want to repeat the same do-while loop with different PHP code inside it. But it doesn't work. It seems as if the system has forgotten the results of the query after doing the first do-while. If I precede the second do-while with a repeat of the original query, it works. But that seems very messy, and surely it is unnecessary to write the same query twice on the same page.
Any advice would be appreciated. Thanks.
Assign the rows to an array then reuse the array
$Offers = array();
while($row_offer = mysql_fetch_assoc($offer)) {
$Offers[] = $row_offer;
}
then further down your code loop over $Offers
after looping the data once, you need to do
mysql_data_seek($offer, 0);
More info: http://php.net/manual/en/function.mysql-data-seek.php
Note: This extension is deprecated as of PHP 5.5.0
Assign the results to an array and iterate over that again, or create function which you can call whenever you need it.
<?php
function getOffers($hid) {
$offers = array();
$query = mysql_query('SELECT ...');
while($offer = mysql_fetch_assoc($query)) {
array_push($offers, $offer);
}
return $offers;
}
$offers = getOffers($hid);
// use $offers here
// or here
// or recall getOffers($hid)
Anthony.
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
}
I have this method in my db class
public function query($queryString)
{
if (!$this->_connected) $this->_connectToDb(); //connect to database
$results = mysql_query($queryString, $this->_dbLink) or trigger_error(mysql_error());
return mysql_num_rows($results) > 0 ? mysql_fetch_assoc($results) : false;
}
This works great for queries that return 1 row, but how can I get an array returned something like this?
$array[0]['name'] = 'jim'
$array[0]['id'] = 120
$array[1]['name'] = 'judith'
$array[1]['ID'] = 121
Now I know I could use a while loop to insert this data into the array like so, but I was wondering if PHP could do this with an internal function? I havn't been able to find on the docs what I'm after.
The reason I don't want to run the while within the method is because I am going to reiterate back over the array when it's returned, and I'd rather not run through the results twice (for performance reasons).
Is there a way to do this? Do I have a problem with my general query method design?
Thank you muchly!
public function query($queryString)
{
if (!$this->_connected) $this->_connectToDb(); //connect to database
$results = mysql_query($queryString, $this->_dbLink) or trigger_error(mysql_error());
$data = array();
while($row = mysql_fetch_assoc($results))
{
$data[] = $row;
}
return $data;
}
this will always return an array.
EDIT:
I didn't read the question well.
If you realy don't want to use the loop then I would do this:
public function query($queryString)
{
if (!$this->_connected) $this->_connectToDb(); //connect to database
return mysql_query($queryString, $this->_dbLink) or trigger_error(mysql_error());
}
then loop over it, however I would just use the loop.
You might also want to look at the PDO extension. You can load the entire result set into an array or you can loop using foreach.
<?php
$db = new PDO($connection_string, $username, $password);
$result = $db->query($queryString);
foreach($result as $row) {
// do something
}
// or
$result = $db->query($queryString);
$result_array = $result->fetchAll(PDO::FETCH_ASSOC);
?>
Most people use a while() loop in the query to do exactly what you want and then loop over the array to process it.
However, you're right: it wastes memory, which could be a problem with a large dataset. An alternative is for your query method to return the resultset resource. Then your while loop can use that to fetch each row as it requires it.
To abstract that away, I would suggest another class to do that for you. Then your query call would return a new instance of that class which has the MySQL resultset resource as an instance variable and packages up the mysql_fetch_assoc() call.
Look at PEAR::MDB2 (Quickstart Cheatsheet). It provides lots of different functions for doing something like this. It also does not tie you down into using MySQL specific functions because it is a database abstraction layer.
$result = $db->queryRow($query, MDB2_FETCHMODE_ASSOC);
There are other abstraction layers such as ADO as well.
thanks for the ideas. I have a function that returns an associative array from the sql (used in Moodle).
$results = get_records_sql($sql);
//to create a numerically indexed array:
$data = array();
foreach ($results as $row)
{
$data[] = $row;
}
return $data;
}