php - PDO prepared statements and their execution -
i have 'upgraded' mysql* pdo, , have couple of related questions:
1/ have form on webpage submits alot of data. rather have 1 looong prepared statement maybe 50 items in it, id split maybe 5 separate statements:
//tods $stmt = $db->prepare("update first_page_data(tod_house, tod_bung, tod_flat, tod_barnc, tod_farm, tod_small, tod_build, tod_devland, tod_farmland) set(?,?,?,?,?,?,?,?,?) email_address=?"); $stmt->bindvalue(1, $_post['tod_house'], pdo::param_str); $stmt->bindvalue(2, $_post['tod_bung'], pdo::param_str); $stmt->bindvalue(3, $_post['tod_flat'], pdo::param_str); $stmt->bindvalue(4, $_post['tod_barnc'], pdo::param_int); $stmt->bindvalue(5, $_post['tod_farm'], pdo::param_str); $stmt->bindvalue(6, $_post['tod_small'], pdo::param_str); $stmt->bindvalue(7, $_post['tod_build'], pdo::param_str); $stmt->bindvalue(8, $_post['tod_devland'], pdo::param_str); $stmt->bindvalue(9, $_post['tod_farmland'], pdo::param_str); $stmt->bindvalue(10, $_session['buyer_email']); $stmt->execute();
this first of 5 blocks. if didnt split statement 50 items long. question there noticable adverse effects splitting up? speed, pressure on server, etc... there 5 smaller updates database rather 1 big one.
2/ second question quite simple - code above considered 'safe'? ive seen people put $_post values variable (mostly because of mysql) , statements. ive read using pdo prevents injections , post values can put straight bindvalue im not entirely sure!
yes, it's safe (pdo::prepare takes care of that), , 1 big statement more efficient series of smaller ones. can script code make less painful read , edit. suggestions:
put query in string , make more vertical, 1 line per parameter. heredoc syntax great , avoids concatenation.
use :whatever in query instead of numbered values, clarity , portability.
instead of calling bindvalue repeatedly, build array , pass whole execute(); arrays simplest thing php can do, , should less overhead calling bindvalue repeatedly, though real difference negligible.
$arrparams[] = array(':tod_house' => $_post["tod_house"];
$stmt->execute($arrparams);
Comments
Post a Comment