The data is being sent from the front end like this:
var data = {
'user_id':userid,
'qid':array[qnum].qid,
'user_ans':userAnswers[qnum].answer,
'user_time':userTime,
'exerciseid':exid,
'point_scored':points
};
$.post('<?php echo base_url()?>main/update_user_score',
{ myData : data },
function(result){} );
And in my "main" controller, I have:
$post_data = $_POST['myData'];
$data = array(
'user_id' => $post_data[user_id] ,
'qid' => $post_data[qid],
'user_ans' => $post_data[user_ans],
'user_time' => $post_data[user_time],
'exerciseid' => $post_data[exerciseid],
'point_scored' => $post_data[point_scored]
);
$this->load->model('Question_model','questions');
$this->questions->update_user_attempt($data);
In my Question_model / update_user_attempt:
error_log("data in model BEFORE INSERT:" . json_encode($data));
$this->db->insert('user_attempt', $data);
error_log("data in model AFTER INSERT: ");
The problem is, the data reaches (at least seems to me) the model quite fine. Here's the log entry:
[23-Apr-2012 16:04:47] data in model BEFORE INSERT:{"user_id":"5","qid":"3","user_ans":"d","user_time":"3","exerciseid":"cr1","point_scored":"35"}
BUT there is NO "after insert" log entry. The insert itself does not happen, and neither does the log entry after the insert.
I can read from the DB quite fine. So I checked in phpmyadmin the user privileges of the user in "config/database.php", and that user has ALL privileges, including INSERT.
So two questions:
What is the problem? What mistake am I making?
How do I even begin finding out what is going on with the insert statement? (I cannot find anything in the logs.)
I am looking at xampp/apache/logs/error.log and xampp/php/logs/php_error_log. Should I be looking at some other logs?
Try putting $this->db->_error_message(); after db insert
like this ...
error_log("data in model BEFORE INSERT:" . json_encode($data));
$this->db->insert('user_attempt', $data);
echo $this->db->_error_message();
error_log("data in model AFTER INSERT: ");
Aren't you missing this
$post_data['user_id']
Instead you are using without quotes
Don't forget to use $this->db->last_query() after your insert statement to see what the insert statement looks like. copy and paste that insert statement into your sql client to see if the query is actually working on its own.
Try adding
ini_set("display_errors", "1");
Also before your insert,
var_dump($data); die();
to see exactly what you are passing. Then check here to see that you are properly forming the insert statment.
First thing I thought was: shouldn't you use json_decode in stead of json_encode?
Did you set error_reporting(E_ALL);?
Related
I think I've worked out that the issue relates to binding params for PDO instead of including the values in the select statement.
Now googling how to bind array values where there will be a variable number of them per query type. For example there may be one, two or three category (col) values sought.
My primary script submits an ajax request to a secondary script.
Two params are sent and are being received in most scenarios.
eg department:mens-fashion, category:coats-and-jackets
In all but a few scenarios, the ajax query is successful and returns as expected. However, for some pairings of params, nothing is displayed in the page or in console.
Having returned from the query script immediately after building the query, I know the query is always working OK. (Copying and pasting into phpMyAmdin brings me the correct resultset.)
So it seems the key part of the script is the fetch routine. How can I catch an error message from PDO in the next line?...
$filtered_results = $filtered_statement->fetchAll(PDO::FETCH_ASSOC);
$debugInfo = array('debug' => vsprintf(str_replace("?", "%s", $filtered_statement->queryString), $opts ));
[edit]
if ( is_array($filtered_results) ) {
$filtered_results = array_merge($debugInfo, $filtered_results);
} else {
$filtered_results = $debugInfo;
}
[/edit]
debug info still only being returned if the query was a success.
$filtered_results_json = json_encode($filtered_results);
echo( $filtered_results_json );
Please would anyone point me to a solution where, the failed query will display in the calling script. The data required is in the db so I am still really stuck on trying to display failure messages.
You need to check whether an array is returned or not. If nothing was found no array is being returned - and you can't use array_merge.
$filtered_results = $filtered_statement->fetchAll(PDO::FETCH_ASSOC);
$debugInfo = array('debug' => vsprintf(str_replace("?", "%s", $filtered_statement->queryString), $opts ));
if ( is_array($filtered_results) ) {
$filtered_results = array_merge($debugInfo, $filtered_results);
} else {
$filtered_results = $debugInfo;
}
$filtered_results_json = json_encode($filtered_results);
echo( $filtered_results_json );
See this as well: Value return when no rows in PDO
this is the code I am working on. I am creating an API for my mobile device.
<?php
$property_id=$_GET['property_id'];
$res_image=mysql_query("select *
from fak_property_image
where property_id='".$property_id."'");
$i=0;
while($row2=mysql_fetch_array($res_image)){
// $status[$i]['property_id']=$row2['property_id'];
// $status[$i]['image']=$row2['image'];
$status[$i]['path']=$row2['path'];
$i++;
}
echo json_encode( $status );exit;
?>
the output is following
[
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0008.1525441844.JPG"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0009.1525441844.JPG"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0013.1525441844.JPG"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0015.1525441844.JPG"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0016.1525441844.JPG"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0074.1525441844.JPG"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0210.1525441844.JPG"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0211.1525441844.JPG"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1400.1525441844.JPG"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1903.1525441844.jpg"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1904.1525441844.jpg"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1905.1525441844.jpg"},
{"path":"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1906.1525441844.jpg"}
]
but my requirement is to display the output like this
[
{
"path":
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0008.1525441844.JPG",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0009.1525441844.JPG",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0013.1525441844.JPG",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0015.1525441844.JPG",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0016.1525441844.JPG",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0074.1525441844.JPG",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0210.1525441844.JPG",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_0211.1525441844.JPG",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1400.1525441844.JPG",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1903.1525441844.jpg",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1904.1525441844.jpg",:
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1905.1525441844.jpg",
"http:\/\/grandthecompany.com\/admin\/uploads\/IMG_1906.1525441844.jpg"
}
]
I have tried to fetch all the data using mysql_fetch_all but it is not working. Please help.
Thanks
Try the following:
$status['path'][] = $row2['path'];
Instead of:
$status[$i]['path']=$row2['path'];
But you definitely should use PDO, besides this code is vulnerable for SQL injections.
I update an existing document with the code below. Its working fine.
foreach($jArray as $value){
!!some code!!
try {
$collection->update(array("tablename"=>$tablename),array('$push' => array("inventar" => $new_data)));
echo json_encode($collection);
}
catch ( MongoConnectionException $e ) {
echo '<p>Update failed</p>';
exit();
}
}
JSON response:
{"w":1,"wtimeout":10000}{"w":1,"wtimeout":10000}
(2 values are tried to update)
Even if no tablename matched, means no update happend, the result is w = 1.
Why? No update happend and w is 1/true?
There seems to be a little confusion here. The JSON response you are looking at is not the actual return value from the update operation. What you did was JSONifying the collection itself, which has the integer attributes w and wtimeout (see source code here). Those attributes are in no way related to the result of the update operation itself.
So, the right way to go seems to be changing the lines inside the scope of your try statement to:
$result = $collection->update(array("tablename"=>$tablename),array('$push' => array("inventar" => $new_data)));
echo json_encode($result);
For more information about what is returned from the update method, refer to these docs.
how do i use getLastError() within php to check if my save method is inserting to mongo?
i set up my db as follows:
$this->databaseEngine = $app['mongo'];
$this->db = $this->databaseEngine->dbname;
$this->collection = $this->db->collectionname;
my insert query then looks like this:
$query = array(
'fieldname' => $fieldname
);
$this->collection->insert($query);
I want to then use getLastError() to check whether it is inserting correctly, and if not why. but im not sure how to implement it.
do i use after the insert:
$this->collection->getLastError("what goes here?");
cheers.
Update
i eventually used this to get the last error:
echo '<pre>' . print_r($this->databaseEngine->lastError(), true) . '</pre>';
Sammaye's way works just as well, see below.
$this->collection->getLastError("what goes here?");
Nothing goes there, the return of the getLastError is the last error from MongoDB ( http://www.php.net/manual/en/mongodb.lasterror.php ). Also it is used on the MongoDB class (atm).
You don't have to use it like that, instead you can do:
$this->collection->insert($query, ARRAY('safe' => TRUE));
This will return an array from the function detailing whether or not it actually inserted. The details of the array can be found by reading this page:
http://www.php.net/manual/en/mongocollection.insert.php
I am working on project where I need to implement SphinxSearch with Cake php. So I am simply trying to use a component and behaviour into it. The link to it, is :-
http://bakery.cakephp.org/articles/eugenioclrc/2010/07/10/sphinx-component-and-behavior
I am requesting Sphinx API like below :
$sphinx = array('matchMode' => SPH_MATCH_ALL, 'sortMode' => array(SPH_SORT_EXTENDED => '#relevance DESC'));
$results = $this->ModelName->find('all', array('search' => 'Search_Query', 'sphinx' => $sphinx));
pr($result);
For above it is working fine ,but when I tried to minimise the response time querying to a particular field of the table (using extended match modes,i.e. SPH_MATCH_EXTENDED2) , Sphinx just fails to output any result. The extended query which I used is given below :-
$sphinx = array('matchMode' => SPH_MATCH_EXTENDED2, 'sortMode' => array(SPH_SORT_EXTENDED => '#relevance DESC'));
$results = $this->ModelName->find('all', array('search' => '#Field_name Search_Query', 'sphinx' => $sphinx));
pr($results);
Can anyone recognise where am I going wrong with it? Please help if I am going wrong some where.
Thanks in advance.
Btw, when you use EXTENDED2 mode make sure your rank mode is set accordingly.
Edit:
Anyway back to you problem, looking at that component/behavior code you can see right away that no error checking is done whatsoever. Try changing the code a bit so you can at least see the errors and/or warnings.
Component
if(!isset($query['search'])){
$result = self::$sphinx->Query('', $indexes);
} else {
$result = self::$sphinx->Query($query['search'], $indexes);
}
if ($result === false) {
// throw new SphinxException();
die(self::$sphinx->GetLastError());
}
$warn = self::$sphinx->GetLastWarning();
if ($warn) echo $warn;
Behavior
$result=$this->runtime[$model->alias]['sphinx']->search($s);
if ($result === false) {
die($this->runtime[$model->alias]['sphinx']->GetLastError());
}
$warn = $this->runtime[$model->alias]['sphinx']->GetLastWarning();
if ($warn) echo $warn;
I hope that helps.
As you said ,
Sphinx just fails to output any result.
That means it's an error :
Please check whether you have added the specific field to the indexing by using sql_query
Also check if the field you are searching for is not an attribute
As per the sphinx documentation :
Attributes, unlike the fields, are not full-text indexed. They are stored in the index, but it is not possible to search them as full-text, and attempting to do so results in an error.