通过一些检查,即使我的主机上安装了mysqli,显然mysqlnd驱动程序也不存在。因此,不能使用get_result()(PHP手册将get_result定义为mysqlnd依赖),取而代之的是,需要做一些额外的编码以按我希望的方式处理我的结果。
因此,我决定尝试并了解PDO的工作原理,几分钟之内,瞧!
将上面的代码替换为:
function checkUsernameEmailAvailability($username, $email) {echo 'pdo about to be created';$dsn = 'mysql:dbname='.C_base.';host='.C_HOST;$user = C_USER;$password = C_PASS;try { $dbh = new PDO($dsn, $user, $password);} catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage();}$params = array(':username' => $username, ':email' => $email);try{$sth = $dbh->prepare('SELECt username,email FROM tb_users WHERe username = :username OR email = :email ');} catch(PDOException $e) { echo 'Prepare failed: ' . $e->getMessage();}try{$sth->execute($params);} catch(PDOException $e) { echo 'Execute failed: ' . $e->getMessage();}$result = $sth->fetch(PDO::FETCH_ASSOC);print_r($result);}
我可以想到的错误检查量不多,而且一开始就完全没有问题!
最终密码
function checkUsernameEmailAvailability($username, $email) { $dsn = 'mysql:dbname='.C_base.';host='.C_HOST; $user = C_USER; $password = C_PASS; new PDO($dsn, $user, $password); $params = array(':username' => $username, ':email' => $email); $sth = $dbh->prepare('SELECt username,email FROM tb_users WHERe username = :username OR email = :email '); $sth->execute($params); $result = $sth->fetch(PDO::FETCH_ASSOC); print_r($result);}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)