What returns R::exec(UPDATE ..... SET ....) if this operation fails?

42 views
Skip to first unread message

Alessio18911

unread,
Mar 9, 2021, 5:35:50 PM3/9/21
to redbeanphp
Hi there! I have a form with some fields on site. It's possible to update database with this form. But it's not necessary to update all of them at once. The docs say that R::exec returns integer. As I saw, 1 - if the field has been changed, 0 - if not. But what the method returns if the query operation fails for some reason? Or R::exec never fails?

gabor

unread,
Mar 9, 2021, 5:37:19 PM3/9/21
to redbeanphp
It will throw an exception you can catch.
Message has been deleted

gabor

unread,
Apr 6, 2021, 7:18:26 AM4/6/21
to redbeanphp
$id  = R::store(R::dispense(['_type'=>'book','title'=>'BeanStory']));
$id2 = R::store(R::dispense(['_type'=>'book','title'=>'a Story about a Bean'])); 
var_dump(R::exec('UPDATE book SET title = ? WHERE id = ? OR id = ?',['BeanStories',$id,$id2]));
var_dump(R::exec('UPDATE book SET title = ? WHERE id = ?',['BeanStories',$id]));
var_dump(R::exec('UPDATE book SET title = ? WHERE id = -1',['BeanStories']));
var_dump(R::exec('UPDATE book SET title = ? WHERE nonexistant_column = -1',['BeanStories']));

SQLite will return

int(2) -- two books updated
int(1) -- one book updated (not really but depends on DB, SQLite says updated 1 although title is same) 
int(0) -- no books have been updated
PHP Fatal error:  Uncaught [HY000] - SQLSTATE[HY000]: General error: 1 no such column: nonexistant_column -- Exception (in frozen mode)

MySQL will return

int(2) -- same
int(0) -- see? MySQL sees it differently
int(0) -- no books, same
PHP Fatal error:  Uncaught [42S22] - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'nonexistant_column' in 'where clause'

cheers
Gabor
Reply all
Reply to author
Forward
0 new messages