mirror of
https://github.com/jspsych/jsPsych.git
synced 2025-05-12 08:38:11 +00:00
parent
37c7966d9c
commit
22748866b3
@ -56,23 +56,15 @@ There are dozens of database options. MySQL is one of the most popular [relation
|
||||
|
||||
### Step 1
|
||||
|
||||
To communicate with a MySQL database, you will need a server-side script, such as a PHP script. The following script should work for all jsPsych data. Copy the code below into a PHP file and give it an appropriate name (e.g. savedata.php). Put the PHP file on your server in a convenient location.
|
||||
To communicate with a MySQL database, you will need a server-side script, such as a PHP script. The following script should work for all jsPsych data. Copy the code below into a PHP file and give it an appropriate name (e.g. savedata.php). Put the PHP file on your server in a convenient location. **This script will only work with jsPsych version 4.0 and later.**
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
// Submit Data to mySQL database
|
||||
// Josh de Leeuw, August 2012
|
||||
// Josh de Leeuw
|
||||
|
||||
// Usage:
|
||||
// You need to POST the following variables to the script (e.g. using jQuery.ajax)
|
||||
// "table": name of the mysql table to insert data into
|
||||
// "json": json-encoded object that contains the data
|
||||
// "opt_data": an associative array of additional variables to write into each row (e.g. subject ID)
|
||||
//
|
||||
//
|
||||
//
|
||||
// EDIT THE FIRST LINE OF CODE BELOW TO CONNECT TO YOUR DATABASE
|
||||
// Edit this line to include your database connection script
|
||||
//
|
||||
// The script you link should contain the following two lines:
|
||||
//
|
||||
@ -94,42 +86,30 @@ function mysql_insert($table, $inserts) {
|
||||
$tab = $_POST['table'];
|
||||
|
||||
// decode the data object from json
|
||||
$trials_obj = json_decode($_POST['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);
|
||||
|
||||
// go through each element of the trials object to see if there is data
|
||||
// (some plugins may not have data associated with them)
|
||||
$trials = $trials_obj[0];
|
||||
for($i=1; $i<count($trials_obj); $i++)
|
||||
{
|
||||
if(count($trials_obj[$i])>0){
|
||||
// if there is data, merge that data into the $trials array
|
||||
$trials = array_merge($trials, $trials_obj[$i]);
|
||||
}
|
||||
}
|
||||
|
||||
// this outputs the trials array as a string. Useful for debugging in the JavaScript console.
|
||||
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);
|
||||
$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());
|
||||
die('Invalid query: ' . mysql_error());
|
||||
} else {
|
||||
print "successful insert!";
|
||||
print "successful insert!";
|
||||
}
|
||||
|
||||
?>
|
||||
|
Loading…
Reference in New Issue
Block a user