sql - PHP PDO sqlsrv executing multiple SELECT COUNT -
i'm @ beginning of pdo learning curve , soaking can learn. please can advise if code below best approach. want find if user has entry in more 1 sql server table
try { $conn = new pdo("sqlsrv:server=$pdoserver;database=$pdodatabase;", $pdouid, $pdopwd); $conn->setattribute( pdo::attr_errmode, pdo::errmode_warning ); } catch (pdoexception $e) { die(error_log(print_r("could not connect sql server".$e->getmessage(), true), 0)); } if (isvalidusername($username)) { $res_sites = $res_pr = $res_ta = array(); try { $stmt_pr = $conn->prepare("select count(*) prcounter tbl1 usr_username=:username"); $stmt_ta = $conn->prepare("select count(*) tacounter tbl2 usr_username=:username"); $stmt_pr->bindparam(':username',$username,pdo::param_str); $stmt_ta->bindparam(':username',$username,pdo::param_str); $stmt_pr->execute(); $stmt_ta->execute(); $res_pr = $stmt_pr->fetch(); $res_ta = $stmt_ta->fetch(); $stmt_sites = $conn2->query("select site_id,site_name tbl3 isdeleted null order site_name asc"); while ($row = $stmt_sites->fetch(pdo::fetch_assoc)){ $res_sites[$row['site_id']]['site_name']=$row['site_name']; } } catch (pdoexception $e) { echo "error occurred. error message: ". $e->getmessage(); } /* more stuff... */ }
as expected, output returned is
$res_pr: array(2) {["prcounter"]=>string(1) "0" [0]=>string(1) "0"} $res_ta: array(2) {["tacounter"]=>string(1) "0" [0]=>string(1) "0"} $res_sites: array(211) {[0]=>array(3) {["site_id"]=>string(1) "1" ["site_name"]=>...}
this 1 of several sections of code i'm working on. pulling row 7 tables when page loads. above fine, or can multiple statements prepared , executed in single execute() statement?
many thanks.
can multiple statements prepared , executed in single execute() statement?
no.
even if possible, wouldn't solve none of problems, because irrelevant pdo, , belongs general database knowledge, such table structure , optimization. say, if put counters in 1 table, you'll surely able them in 1 query. , if create index username, speed things up.
Comments
Post a Comment