 |
|
|
| View previous topic :: View next topic |
| Author |
Message |
chris8726
Joined: 19 Mar 2008 Posts: 9
|
Posted: Tue Mar 25, 2008 9:24 pm Post subject: Script using AutoCommit |
|
|
I am trying to learn from a book I bought about MySQL, but the book I'm reading is using PEAR which I am not familiar with. It is also talking about AutoCommit for InnoDB tables (something my shared server doesn't support). I need helping taking all the AutoCommit junk out of the script so that it runs for regular tables. If anyone can hel out that would be great. Here is the code:
| Code: |
function enter_scores ($conn)
{
$event_id = script_param ("event_id");
$score = script_param ("score");
if (!preg_match ('/^\d+$/', $event_id)) die ("Bad event ID");
$sth_del = $conn->prepare ("DELETE FROM score
WHERE event_id = ? AND student_id = ?");
if (DB::isError ($sth_del))
die ("Cannot prepare statement");
$sth_repl = $conn->prepare ("REPLACE INTO score
(event_id,student_id,score)
VALUES(?,?,?)");
if (DB::isError ($sth_repl))
die ("Cannot prepare statement");
if ($conn->autoCommit (FALSE) != DB_OK)
die ("Cannot disable autocommit");
$err = 0;
$blank_count = 0;
$nonblank_count = 0;
reset ($score);
while (list ($student_id, $new_score) = each ($score))
{
$new_score = trim ($new_score);
if (empty ($new_score))
{
++$blank_count;
$sth =& $sth_del;
$params = array ($event_id, $student_id);
}
else if (preg_match ('/^\d+$/', $new_score)) # must look like integer
{
++$nonblank_count;
$sth =& $sth_repl;
$params = array ($event_id, $student_id, $new_score);
}
else
{
print ("invalid score: $new_score");
$err = 1;
break;
}
if ($conn->execute ($sth, $params) != DB_OK)
{
print ("score entry failed, event_id $event_id,"
. " student_id $student_id");
$err = 1;
break;
}
}
if ($err == 0)
{
if ($conn->commit () != DB_OK)
die ("Cannot commit transaction");
}
else
{
if ($conn->rollback () != DB_OK)
die ("Cannot roll back transaction");
}
$conn->autoCommit (TRUE);
$conn->freePrepared ($sth_del);
$conn->freePrepared ($sth_repl);
printf ("Number of scores entered: %d<br />\n", $nonblank_count);
printf ("Number of scores missing: %d<br />\n", $blank_count);
print ("<br />\n");
}
|
I don't know if it is an AutoCommit problem or not for sure, but that is all I can think of. Also, here is the error I get back:
| Code: |
[25-Mar-2008 19:32:33] PHP Warning: reset(): Passed variable is not an array or object in /GP/score_entry.php on line 245
[25-Mar-2008 19:32:33] PHP Warning: Variable passed to each() is not an array or object in /GP/score_entry.php on line 246
|
Thanks for any help! |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1048
|
Posted: Tue Mar 25, 2008 10:35 pm Post subject: |
|
|
The (not) auto commit calls that you can remove are two blocks:
| Code: |
if ($conn->autoCommit (FALSE) != DB_OK)
die ("Cannot disable autocommit");
|
and
| Code: |
if ($err == 0)
{
if ($conn->commit () != DB_OK)
die ("Cannot commit transaction");
}
else
{
if ($conn->rollback () != DB_OK)
die ("Cannot roll back transaction");
}
|
The error messages that you've got aren't related to this committing feature, they are caused by this line:
| Code: |
$score = script_param ("score");
|
I don't know what this scrip_param() method does, but this seems to be the right place to start debugging. |
|
| Back to top |
|
 |
chris8726
Joined: 19 Mar 2008 Posts: 9
|
Posted: Tue Mar 25, 2008 11:16 pm Post subject: |
|
|
Okay, here is the script_param () function code:
| Code: |
function script_param ($name)
{
global $HTTP_GET_VARS, $HTTP_POST_VARS;
unset ($val);
if (isset ($_GET[$name]))
$val = $_GET[$name];
else if (isset ($_POST[$name]))
$val = $_POST[$name];
else if (isset ($HTTP_GET_VARS[$name]))
$val = $HTTP_GET_VARS[$name];
else if (isset ($HTTP_POST_VARS[$name]))
$val = $HTTP_POST_VARS[$name];
if (isset ($val) && get_magic_quotes_gpc ())
$val = remove_backslashes ($val);
return (@$val);
}
|
I'm not 100% sure what it does. Like I said I am trying to learn. The book claims that $score will be returned as an array, but my server seems to think otherwise. Let me know if you see anything that could be changed. Thanks. |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1048
|
Posted: Wed Mar 26, 2008 11:11 am Post subject: |
|
|
Such a function is not really related to this forum, as it isn't related to PEAR. Therefore, only a short answer: The function will only return an array if there is a POST or GET parameter that is an array, e.g. index.php?score[]=1&score[]=2. If you have only e.g. index.php?score=3, $score won't be an array, but just simply an integer or a string.
How old is this book? Using $HTTP_GET_VARS and $HTTP_POST_VARS isn't really good style anymore, at least since PHP 5, and maybe already since PHP 4.3. |
|
| Back to top |
|
 |
chris8726
Joined: 19 Mar 2008 Posts: 9
|
Posted: Wed Mar 26, 2008 7:54 pm Post subject: |
|
|
| The book is from 2005. Where do you think I should ask this question? |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1048
|
Posted: Wed Mar 26, 2008 8:25 pm Post subject: |
|
|
| chris8726 wrote: | | The book is from 2005. |
I'd consider reading a more up-to-date book. IMHO it's annoying to work with such outdated code.
| chris8726 wrote: | | Where do you think I should ask this question? |
Several places are mentioned on this site:
http://www.php.net/support.php |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|