php - converting from mysql to PDO username confirmation issue

Solution:
You'd probably want to do this with a prepared statement
$u_check = $pdo->prepare("SELECT username FROM users WHERE username= :username");
$u_check->execute(array(':username' => $un));
if($u_check->rowCount()) {
Answer
Solution:
I agree with Machavity about the prepared statement:
$u_check = $pdo->prepare("SELECT username FROM users WHERE username= :username");
$u_check->execute(array(':username' => $un));
But for a portable count of selected rows, you should prefer:
$rows=$u_check->fetchAll();
if(count($rows)) {...
see http://php.net/manual/en/pdostatement.rowcount.php for details on restriction in the use ofrowCount()
for select queries (portability problems, depends on underlying DB).
Answer
Solution:
Always bind parameters in PDO with proper type definition to avoid PDO predictive type process. Avoid execute with array as possible as this imply predictive PDO process.
Use PDO rowCount before the fetch (faster than anything else).
$u_check = "SELECT username FROM users WHERE username=?"
$stmt = $pdo->prepare($u_check);
$stmt->bindParam(1, $un, PDO::PARAM_STR);
$stmt->execute();
$count = $stmt->rowCount();
if($count>=1){
$result= $stmt->fetchall(PDO::FETCH_ASSOC);
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: xml parsing error: no root element found
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.