/    Sign up×
Community / Pin to Profile Bookmark

Converting PDO OOP To Mysqli Procedural

Folks, ?

My 3+ pages are all in mysqli procedural. I cannot just switch to pdo and oop and throw 6 mnths of work down the drain! And so, let’s try converting the following code suggestion by Death Shadow to mysqli procedural.

[code]
if (
array_key_exists(‘login_username_or_email’, $_POST) &&
array_key_exists(‘login_password’], $_POST)
) {

// don’t bother trimming, they can’t enter it right, don’t let them log in!

$stmt = $conn->prepare(‘
SELECT ids, usernames, passwords, emails, accounts_activations_statuses
FROM users
WHERE ‘ . (
strpos($usernameOrEmail, ‘@’) === false) ? ‘usernames’ : ’emails’
) . ‘ = ?
‘);
$stmt->bind_param(‘s’, $_POST[‘login_username_or_email’]);
$stmt->execute();
$stmt->bind_result(
$db_id, $db_username, $db_password, $db_email,
$db_account_activation_status
);

if (
$stmt->fetch() &&
password_verify($_POST[‘login_password’], $db_password)
) {
echo ‘
<p>Login Successful</p>
<dl>
<dt>User Id</dt>
<dd>’, $db_id, ‘</dd>
<dt>E-Mail</dt>
<dd>’, $db_email, ‘</dd>
<dt>Username</dt>
<dd>’, $db_username, ‘</dd>
<dt>Activation Stats</dt>
<dd>’, $db_account_activation_status, ‘</dd>
</dl>
‘;
} else echo ‘<p>Invalid username or password</p>’;

$stmt->close();

} else echo ‘<p>Missing username or password</p>’;
[/code]

I need your help.
Remember, the script is a login page and the user is given a choice to either type his email or username. And then finally the password.
The html form looks like this:

[code]
<!DOCTYPE html>
<html>
<head>
<title><?php $site_name?> Member Login Page</title>
<meta charset=”utf-8″>
</head>
<body>
<form method=”post” action=””>
<h3><?= $site_name ?> Member Login Form</h3>
<fieldset>
<label for=”login_name”>Username/Email:</label>
<input type=”text” name=”login_username_or_email” id=”login_name” value=”<?php if(isset($_COOKIE[“login_username_or_email”])) echo $_COOKIE[“login_username_or_email”]; ?>”
<br>
<label for=”login_pass”>Password:</label>
<input type=”password” name=”login_password” id=”login_pass” value=”<?php if(isset($_COOKIE[“login_password”])) echo $_COOKIE[“login_password”]; ?>”>
</fieldset>
<div class=”submitsAndHiddens”>
<label for=”login_remember”>Remember Login Details:</label>
<input type=”checkbox” name=”login_remember” />
<br>
<button type=”submit”>Login</button>
<br>
<a href=”login_password_reset.php”>Forgot your Password ? Reset it here!</a>
<br>
<a href=”register.php”>Register here!</a>
</div>
</form>

</body>
</html>
[/code]

The following is regex to check if the user typed email or not.
Newbies, if you were after a regex that checks if the input is email or not. Then, here it is:

[code]
function valid_email($email) {
if(preg_match(‘/^([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])*$/’,$email)) {
return TRUE;
} else {
return FALSE;
}
}
[/code]

I need help adding the above regex in the appropriate place in the script. Appropriate integrationing.

?

to post a comment
PHP

6 Comments(s)

Copy link Tweet this Alerts:
@criterion9 Sep 29.2017 — I often refactor things when I find better ways to accomplish them. Just because some things are in one state doesn't mean the whole thing has to be the same (though too much of that and never returning to standardize can leave maintenance looking rough).

It looks like you have the pieces and just need to fit them together. I like to start with a pencil and paper and write out the logic I'm trying to follow whenever I get stuck with something.
Copy link Tweet this Alerts:
@ginerjm Sep 29.2017 — You have some script(s) that use PDO and you want to revert them to MySqlI ?

Why? They work now, don't they? Why change them? The type of interface you use doesn't affect the data one bit. Plus it would be a TREMENDOUS waster of resources, yours and this forum's, to put one second into doing that.
Copy link Tweet this Alerts:
@uniqueideamanauthor Sep 29.2017 — You have some script(s) that use PDO and you want to revert them to MySqlI ?

Why? They work now, don't they? Why change them? The type of interface you use doesn't affect the data one bit. Plus it would be a TREMENDOUS waster of resources, yours and this forum's, to put one second into doing that.[/QUOTE]


No. No. No! You did not understand. For 6 months, I've been learning (& forgetting, & re-learning) and building my pages in mysqli procedural. Someone in another forum pointed-out my mistake in the login.php and showed an example in pdo oop instead. The guy is rare there and I'm feeling bad to ask him to now show me his code in the mysqli procedural style. Hence, I need to convert his code to mysqli. Something that I understand.
Copy link Tweet this Alerts:
@ginerjm Sep 30.2017 — As you stated: No, No, No!

It's already written so why re-write it? As I said, if it works leave it alone. It just doesn't matter if one script uses the PDO interface and the other(s) use MySqlI.

You worry about all the wrong stuff.
Copy link Tweet this Alerts:
@uniqueideamanauthor Oct 02.2017 — As you stated: No, No, No!

It's already written so why re-write it? As I said, if it works leave it alone. It just doesn't matter if one script uses the PDO interface and the other(s) use MySqlI.

You worry about all the wrong stuff.[/QUOTE]


Yes, but then I never learn how the login.php was built.

I've learnt how the registration.php and account_activation.php was built since they were built in mysqli procedural.

I failed to build the login.php properly and someone was kind enough to do it for my and other newbies' learning purpose but in pdo. I'm not learning anything from it becaue I do not understand pdo atall. I'm planning to hop onto it in a month's time after I finish learning mysqli procedural first. And so, the sooner I get the login.php done in mysqli procedural then the sooner I learn to build a login processor script and then move-onto pdo.

I've already written 3 different pages in mysqli procedural and once I finish this login.php then my project to build a member reg-login site with mysqli procedural is complete. Then the next task is to build the pdo version. Since I've already been giving a sample in pdo for the login.php then all I have to do is read enough on pdo and then build my other pages in pdo (registration.php, account_activation.php, home.php, logout.php).
Copy link Tweet this Alerts:
@ginerjm Oct 03.2017 — If you simply looked at (looked, not even 'studied') a PDO example showing the query itself, the prepare statement and the creation of the needed array of parameters followed by the execute statement, you could probably convert your other scripts to PDO just as fast as you struggle to convert the gift you have been given already.

<i>
</i>
$q = "select field,field,field from table where id=:selected_id";
$qst = $pdo_conn-&gt;prepare($q);
$parms = array(':selected_id'=&gt;$_GET['id']);
$result = $qst-&gt;execute($parms);
if (!$result)
{
(handle false result)
}
while($row = $qst-&gt;fetch())
{
(handle results)
]


Now tell me that this isn't easy to pick up.
×

Success!

Help @uniqueideaman spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with Twitch Sign in with Github Create Account
about: ({
version: 0.1.9 BETA 11.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Balmasexy,
tipped: article
amount: 1000 SATS,

tipper: @mbsaad,
tipped: article
amount: 1000 SATS,

tipper: @mbsaad,
tipped: article
amount: 1000 SATS,
)...