Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to migrate mysqli to pdo
    primarykey
    data
    text
    <p>Hi I was wondering how I would migrate a mysqli php file to use PDO. Would anyone be able to take a look at my code and see if I'm on the right track?</p> <p>This is my original (mysqli) code:</p> <pre><code>&lt;?php // connecting to database $conn = new mysqli('xxxxxx', 'xxxxxx', 'password', 'xxxxxx'); $match_email = 'email'; $match_passhash = 'passhash'; if (isset($_POST['email'])) { $clean_email = mysqli_real_escape_string($conn, $_POST['email']); $match_email = $clean_email; } if (isset($_POST['passhash'])) { $clean_passhash = mysqli_real_escape_string($conn, $_POST['passhash']); $match_passhash = sha1($clean_passhash); } $userquery = "SELECT email, passhash, userlevel, confirmed, blocked FROM useraccounts WHERE email = '$match_email' AND passhash = '$match_passhash' AND userlevel='user' AND confirmed='true' AND blocked='false';"; $userresult = $conn-&gt;query($userquery); if ($userresult-&gt;num_rows == 1) { $_SESSION['authorisation'] = 'knownuser'; header("Location: userhome.php"); exit; } else { $_SESSION['authorisation'] = 'unknownuser'; header("Location: userlogin.php"); exit; } ?&gt; </code></pre> <p>And this is my attempt to migrate it to PDO:</p> <pre><code>&lt;?php // connecting to database $dbh = new PDO("mysql:host=xxxxxx; dbname=xxxxxx", "xxxxxx", "password"); $match_email = 'email'; $match_passhash = 'passhash'; if (isset($_POST['email'])) { $clean_email = mysqli_real_escape_string($conn, $_POST['email']); $match_email = $clean_email; } if (isset($_POST['passhash'])) { $clean_passhash = mysqli_real_escape_string($conn, $_POST['passhash']); $match_passhash = sha1($clean_passhash); } $userquery = "SELECT email, passhash, userlevel, confirmed, blocked FROM useraccounts WHERE email = ':match_email' AND passhash = ':match_passhash' AND userlevel='user' AND confirmed='true' AND blocked='false';"; $stmt = $dbh-&gt;prepare($query); $stmt-&gt;bindParam(":match_email", $match_email); $stmt-&gt;bindParam(":match_passhash", $match_passhash); $stmt-&gt;execute(); $userresult = $conn-&gt;query($userquery); if ($userresult-&gt;num_rows == 1) { $_SESSION['authorisation'] = 'knownuser'; header("Location: userhome.php"); exit; } else { $_SESSION['authorisation'] = 'unknownuser'; header("Location: userlogin.php"); exit; } ?&gt; </code></pre> <p>I'm also not sure how to count the number of rows returned in PDO.</p> <p>If anyone would be able to help me out that wold be very great.</p> <p>A million thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload