Forum OpenACS Q&A: Response to 0or1 row Fails with Zero Rows. Why?

Collapse
Posted by Pascal Scheffers on
I am afraid it is supposed to do that. From the AOLServer ns_db API doc:
ns_db 0or1row expects the SQL to be a select statement that returns exactly zero or one row. On zero rows, a null string is returned. On one row, a newly allocated ns_set is returned.
You need to check to see if the string is not empty before doing set_variables_after_query.
set selection [ns_db 0or1row $db "select
  first_names,
  last_name
from users
where user_id=$user_id"]
set_variables_after_query

if [empty_string_p $selection] {
   ad_return_error "Account Unavailable" "user_id $user_id not found"
   ns_log Notice "selection EMPTY"
   return
} else {
  set_variables_after_query
  ns_log Notice "dallas... selection: $selection"
  ns_log Notice "dallas... $last_name, $first_names"
}
ns_db releasehandle $db

Also, you can use ad_get_user_info to set the variables you are trying to obtain, but make sure user_id exists before you execute it (use ad_verify_and_get_user_id or something, they return 0 if the user doesn't exist)