mysql vs. mysqli functions

116 views
Skip to first unread message

William Aue

unread,
Mar 17, 2016, 4:06:30 PM3/17/16
to jsPsych
I am trying to write to a mysql database using the code in the documentation (here), but I'm not having much luck. I'm told that I need to switch my mysql commands to mysqli, but it doesn't seem to be as simple as adding an "i" to the function names and I'm having trouble tracking down my mistake. My edited code is below. Any suggestions are appreciated.

<?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('database_connect.php');

// You should not need to edit below this line

function mysql_insert($table, $inserts) {

include('database_connect.php');
  $values = array_map('mysqli_real_escape_string', array_values($inserts)); $keys = array_keys($inserts); return mysqli_query($dbc, '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: ' . mysqli_error()); } else { print "successful insert!"; } ?>
 

William Aue

unread,
Mar 18, 2016, 3:45:46 AM3/18/16
to jsPsych
I think I figured it out via this solution. In case anyone else ends up needing it, here is the edited code that works for me. I've highlighted all of the changes that I made.

<?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('database_connect.php');

// You should not need to edit below this line

function mysql_insert($table, $inserts) {
    include('database_connect.php');
    $values = array_map(array($dbc, 'real_escape_string'), array_values($inserts));
    $keys = array_keys($inserts);

    return mysqli_query($dbc, '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: ' . mysqli_error());
} else {
    print "successful insert!";
}

?>

Josh de Leeuw

unread,
Mar 19, 2016, 2:41:04 PM3/19/16
to William Aue, jsPsych
Thanks for posting this. My php code is pretty out of date. I'll try to get the documentation updated; or feel free to modify it yourself and submit a pull request!

--
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/c54b0f67-832f-4051-bb31-76ae97fbf72a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages