difficulty writing data to mySQL database

466 views
Skip to first unread message

mwk...@nyu.edu

unread,
Jan 19, 2016, 3:42:14 PM1/19/16
to jsPsych
Hi there,

I've written an experiment that is pretty satisfactory until the point of writing the data to a local server (the mySQL preference panel indicates that the server is running) and I've prepared a database and a table with the headers needed to write jsPsych data into.

In the same folder as my experiment, I also have i) database_connect.php and ii) savedata.php, and a save_data function defined in the js section.

I'm trying to write data to a mySQL database on a local server, before moving on to an actual university server that runs php. I believe I've followed the instructions to a T, but I am unclear how to catch the error. The code below runs all the way to save the data locally (as a .csv file download), so I believe the 'save_data' function is being evaluated; alas, I have no indication at *which* point the mySQL connection is failing. Any help would be much appreciated!

Below is my js snippet:

/* save data code */
function save_data(data){
var data_table = "peacock"; // change this for different experiments
$.ajax({
type:'post',
cache: false,
url: 'savedata.php', // change this to point to your php file.
data: {
table: data_table,
json: JSON.stringify(data),
},
success: function(output) { console.log(output); } // write the result to javascript console
});
}


/* start the experiment */
jsPsych.init({
timeline: timeline,
on_finish: function(data) {
var alldata = jsPsych.data.getData();
jsPsych.data.displayData('CSV');
save_data(alldata);
jsPsych.data.localSave('mydata.csv', 'csv');
}

Josh de Leeuw

unread,
Jan 19, 2016, 3:49:56 PM1/19/16
to mwk...@nyu.edu, jsPsych
Are you getting any error messages in the console? The PHP script should echo out an error message that is sometimes useful if it is running.

--
You received this message because you are subscribed to the Google Groups "jsPsych" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jspsych+u...@googlegroups.com.
To post to this group, send email to jsp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jspsych/91f21573-aabf-4e7c-9d4f-11d8143c7a1c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nastasia Griffioen

unread,
Aug 22, 2016, 10:21:30 AM8/22/16
to jsPsych, mwk...@nyu.edu
Hi both,

Has this problem been solved?

I'm running into a similar issue, having created the save_data.php and database_connect.php files and (I think) having adapted the necessary bits. I've also added in the save_data snippet in my script.
However, after having run the script from our server, I don't find anything stored in the mySQL database.

Also, I'm not sure whether the table one has to create in mySQL should be specified any further (in terms of number of columns and their names) rather than simply by name.

Finally, on an unrelated note, what is this console you talk about? I simply test-run my script by opening it in my browser, so there's no console in my case. Am I doing something the wrong way?

Here is the script I've used.
The bit of script in my experiment code itself:
 function save_data(data){
     
var data_table = "rhythm_data"; // change this for different experiments

     $
.ajax({
        type
:'post',
        cache
: false,

        url
: 'http://research.petervavra.com/rhythm/save_data.php', // change this to point to your php file.
        data
: {
            table
: research_rhythm_data,

            json
: JSON.stringify(data),
       
},
        success
: function(output) { console.log(output); } // write the result to javascript console
     
});
 
}

The script in save_data.php:
<?php
// the $_POST[] array will contain the passed in filename and data
// the directory "data" is writable by the server (chmod 777)
$filename
= "data/".$_POST['filename'];
$data
= $_POST['filedata'];
// write the file to disk
file_put_contents
($filename, $data);
?><?php

// Submit Data to mySQL database
// Josh de Leeuw

// Edit this line to include your database connection script
//
//  The script you link should contain the following two lines:
//
//  $dbc = mysql_connect('localhost', 'username', 'password');
//  mysql_select_db('databasename', $dbc);
//
include
('http://research.petervavra.com/rhythm/database_connect.php');

// You should not need to edit below this line

function mysql_insert($table, $inserts) {
    $values
= array_map('mysql_real_escape_string', array_values($inserts));
    $keys
= array_keys($inserts);

   
return mysql_query('INSERT INTO `'.$table.'` (`'.implode('`,`', $keys).'`) VALUES (\''.implode('\',\'', $values).'\')');
}

// get the table name
$tab
= $_POST['table'];

// decode the data object from json
$trials
= json_decode($_POST['json']);

// get the optional data (decode as array)
$opt_data
= json_decode($_POST['opt_data'], true);
$opt_data_names
= array_keys($opt_data);

var_dump
($trials);

// for each element in the trials array, insert the row into the mysql table
for($i=0;$i<count($trials);$i++)
{
    $to_insert
= (array)($trials[$i]);
   
// add any optional, static parameters that got passed in (like subject id or condition)
   
for($j=0;$j<count($opt_data_names);$j++){
        $to_insert
[$opt_data_names[$j]] = $opt_data[$opt_data_names[$j]];
   
}
    $result
= mysql_insert($tab, $to_insert);
}

// confirm the results
if (!$result) {
   
die('Invalid query: ' . mysql_error());
} else {
   
print "successful insert!";
}

?>

I'm not sure where things go wrong: is there something more I need to define about my table on my mySQL server itself, or have I defined the paths the wrong way? Simply not a clue.

Any help is once again very greatly appreciated! (:

Best,
Nastasia



Op dinsdag 19 januari 2016 21:49:56 UTC+1 schreef Josh de Leeuw:

Josh de Leeuw

unread,
Aug 22, 2016, 10:42:57 PM8/22/16
to Nastasia Griffioen, jsPsych, mwk...@nyu.edu
Hi Nastasia.

1) Yes, you do need to specify the table fully in MySQL before you can write data to it. I would suggest reading a quick primer on MySQL to familiarize yourself with the basic concepts. There are MySQL GUIs available (MySQL Workbench, HeidiSQL) that make this easy. (We're working on a solution that will make this part of setting up an experiment a lot easier, but it's not quite ready yet).

2) The "console" is the developer console that you can use to interact with a webpage. All the major web browsers include this feature. If you google for <browser> + "developer console" you should get helpful instructions. There are also loads of other debugging tools that can be really helpful, and you can usually get information about them in the same place that describes the console.

Hope that helps!
Josh

Reply all
Reply to author
Forward
0 new messages