In my mongo database > sessions collection, every record has a fb_id and ts (timestamp). I want to fetch the least timestamp for a particular fb_id. The following query worked successfully in mongoDB shell:
db.sessions.find({fb_id: "88877"},{fb_id: 1,ts: 1}).sort({ts:-1}).limit(1) // works perfectly
I need this functionality in my PHP script, so I wrote following line of code:
echo $tracking->sessions->find(array("fb_id"=> $document["fb_id"]),array("ts"=>1))->sort(array("ts"=>-1))->limit(1)
However, I am getting following error:
PHP Catchable fatal error: Object of class MongoCursor could not be converted to string
What is the correct way to fetch this data?
You need to extract row from the cursor first:
$cursor = $tracking->sessions->find(array("fb_id"=> $document["fb_id"]),array("ts"=>1))->sort(array("ts"=>-1))->limit(1);
print_r($cursor->getNext());
Related
I want to load data from a SQLite database in PHP using Atlas.Orm.
When I run the following snippet, I get a set of 1773 results, but each result is the same!
$atlas = Atlas::new('sqlite:[Path To Database]');
$result = $atlas->select(Stop::class)->fetchRecords();
Can anyone tell me whats wrong here?
After several hours of desperation, I found the issue by myself. The ORM needs the PRIMARY_KEY constant in the corresponding *Table class to be set, otherwise fetching records will fail like this.
I am trying to extract from my php laravel some data like this:
$x = Carbon::now()->timestamp;
$data=Notifications->where('happened_at','>',$x)
->where('id_user',Auth::user()->getAuthIdentifier());
If I enter this sql statement in my oracle database directly it works perfectly:
select * from notifications where happened_at > '10-06-2017 00:11:12,000000000';
^ this returns correct, however in my php laravel it doesn't return the good rows(returns all the rows from the database)
Later Edit: The where is the problem, I just want to compare the timestamp location in my DB('happened_at') with the current time...I don't know how though
Let's break down your model call code.
// first, you retrieve all notifications and assign them to $data
$data = Notifications::all()
// then, you try to start a new query
->where('happened_at','>',$x)
// then, you continue the new query
->where('id_user',Auth::user()->getAuthIdentifier())
// and finally, you finish the call
;
So basically, you already get all rows at the beginning, and then you try to start building a query that you never execute. (But you can't, since all() returns a collection.)
Remove the all() and finish up with ->get() at the end, and it should work. (Although I don't know anything about Oracle timestamps.)
I have a stored procedure in sql server.When I try to use the php function to get the results,its not working.
echo mssql_query(exec MOB_uspcommunication_details $userID,'send');
This gives result as 1 instead of resourceID.When I run the query in sql server its giving me the result.
When I change the second parameter to some other value like this,its working from PHP side.
echo mssql_query(exec MOB_uspcommunication_details $userID,'inbox');
This gives me resourceid#10 and I am getting the results.
For the first query,I am not able to understand why this happens.Please help me
From the PHP documentation:
Return Values
Returns a MS SQL result resource on success, TRUE if no rows were returned, or FALSE on error.
So that means that the first function call returns 1 to indicate there were no rows (1 == true in PHP). The second function call actually retrieves the rows from your table.
I am getting oci_execute(): ORA-01843: not a valid month invalid. Why it is giving it?
INSERT INTO DETAIL (PPD_ID, PPD_ID,PPD_ENTRY_DATE, PPD_IGM_ID, PPD_DFM_ID, PPD_DRM_ID, PPD_HEIGHT, PPD_UOM_ID, PPD_FTM_ID, PPD_FRQ_ID, PPD_START_DATE_TIME, PPD_END_DATE_TIME, PPD_STATUS, PPD_STM_ID)
VALUES ('WS00318229',2440900,'29-12-2015',350,1,1,50,46,1,1,'29-12-2015','29-12-2015','PRESCRIBED',6)
When i am using the above query from php to insert record in to oracle database it is giving following error.
oci_execute(): ORA-01843: not a valid month invalid
But when i use the following query it is not giving any error and it is inserting record.How to solve this? This error was already is there but i am not clear about the answers.Any ideas why it is like this.
INSERT INTO DETAIL (PPD_ID, PPD_ID,PPD_ENTRY_DATE, PPD_IGM_ID, PPD_DFM_ID, PPD_DRM_ID, PPD_HEIGHT, PPD_UOM_ID, PPD_FTM_ID, PPD_FRQ_ID, PPD_START_DATE_TIME, PPD_END_DATE_TIME, PPD_STATUS, PPD_STM_ID)
VALUES ('WS00318229',2440900,'29-DEC-2015',350,1,1,50,46,1,1,'29-DEC-2015','29-DEC-2015','PRESCRIBED',6)
You are inserting strings into DATE columns. When you do that, oracle performs implicit conversion based on your NLS settings, specifically, nls_date_format. Evidently when you connect from php script and from some client you use (SQL*Plus, SQL Developer, what have you), your NLS settings differ.
You can check session parameters using following query.
SELECT * FROM nls_session_parameters;
For date presented in format '29-DEC-2015' nls_date_format should be 'DD-MON-YYYY'. If it has some other value, you can change it using following query.
alter session set nls_date_format='DD-MON-YYYY'
Or you can explicitly convert string to date using TO_DATE function and passing format as second parameter, but this will require your application code to be consistent in using only specified date format.
TO_DATE('29-DEC-2015', 'DD-MON-YYYY')
this query works fine using the php_mssql driver:
INSERT INTO Table(columnName) VALUES ('text');
SELECT SCOPE_IDENTITY() AS id;
Table does have an id column, which is an identity.
I would execute that query, and get the last id in the table.
The same code doesn´t work if the query is executed using Microsoft´s php_sqlsrv driver.
I don´t get any error when executing the query (sqlsrv_query function) , but i get the following error when calling sqlsrv_fetch_array:
"The active result for the query contains no fields"
I´ve googled a lot, and didn´t find no answer, it was a big surprise for me that no one faced this problem before, it seems like nobody is using this driver, even though is the "official" one since PHP 5.3 release...
Thanks.
In the initial CTP the field indices started at 1. Later they were changed to start at 0.
Try something like this:
// connection to the dbserver
$result = sqlsrv_query("INSERT INTO Table(columnName) VALUES ('text'); SELECT SCOPE_IDENTITY() AS ID");
echo "The last insert_id is".lastId($result);
function lastId($queryID) {
sqlsrv_next_result($queryID);
sqlsrv_fetch($queryID);
return sqlsrv_get_field($queryID, 0);
}