5

In my code Im trying to translate mysql_real_escape_string into a PDO statement. Does somebody have a tip on how to write mysql_real_escape_string in PDO?

Im using mysql_real_escape_string in two lines: $userName = mysql_real_escape_string($_POST['username']); $password = sha1(mysql_real_escape_string($_POST['password']));

<?php
ob_start();
session_start();
include("../database/db.php");

$userName = mysql_real_escape_string($_POST['username']);
$password = sha1(mysql_real_escape_string($_POST['password']));
echo "<br>user: " . $userName;
echo "<br> pw: " . $password;

$query = "select * from tbladmin where admin_usr_name='$userName' and admin_pwd='$password'";

$res = mysql_query($query);

// $rows = $res->fetch(PDO::FETCH_ASSOC); 
$rows = mysql_fetch_assoc($res);
echo "<br>numrows" . mysql_num_rows($res) . "<br>";

// $find = $query->prepare("select * from tbladmin where admin_usr_name='$userName' and admin_pwd='$password'");
// $find->execute();
// if ($find->fetchColumn() > 0)
// {
// echo 'You made it, welcome';
//  $_SESSION['userName']  =  $rows['admin_usr_name'];
//  $_SESSION['admin_id'] = $rows['admin_id'];
//  header("location: ../pages/content.php");
// }
// else
// {
//  echo 'Username and password dont match <br/> Try again';
//  header("location: ../index.php?loginerror=yes");
// } 

if(mysql_num_rows($res)>0)
{
    $_SESSION['userName']  =  $rows['admin_usr_name'];
    $_SESSION['admin_id'] = $rows['admin_id'];
    header("location: ../pages/content.php");
}
else
{
    echo 'Username and password dont match <br/> Try again';
    header("location: ../index.php?loginerror=yes");
} 

?>

This is what Im trying to do with PDO

  $host = "localhost"; 
$user = "root"; 
$password = "root";
$db = "blog";

$dsn = "mysql:host=$host;dbname=$db;charset=utf8";
$opt = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC);
$pdo = new PDO($dsn,$user,$password, $opt);

$username = $_POST['username'];
$password = $_POST['password'];
$query = "select * from tbladmin where admin_usr_name=:userName and admin_pwd=:passWord";

try 
{   
$databas = new PDO($dsn, $user, $password);
} 
catch (PDOException $e) 
{
echo 'Connection failed: ' . $e->getMessage();
}

$statement = $databas->prepare($query);

$statement->execute(array(':userName'=>$username, ':passWord'=> $password)); 
$row = $statement->fetch();

I always get this error: Call to a member function prepare() on a non-object

Bruno Chavez
  • 193
  • 3
  • 5
  • 15

3 Answers3

8

The point is by using prepared statements with parameterised queries and bound values you don't need things such as mysql_real_escape_string.

Check out the PDO documentation on how to use bound values and parameterised queries/prepared statements.

The point is that you would write an SQL query as such:

$query = $pdo->prepare("SELECT * FROM users WHERE username = ? and password = ?");

You would then pass in the bound values in place of the ? symbols, thus the query is only ever run literally as so:

$query->bindParam(1, $username);
$query->bindParam(2, $password);

Any sort of SQL injection attempt such as 1'; DROP users -- inside the variables above would no longer work as it would be called literally.

Peter Featherstone
  • 7,835
  • 4
  • 32
  • 64
4

You don't need to in PDO. At least, rarely do you need to.

cygorx
  • 228
  • 1
  • 2
  • 19
1

As stated before, you don't have to use escaping... because this is handled more securely by binding values with ? or naming with colons in the sql-statement

I would do something like this:

<?php
$username = $_POST['username'];
$password = $_POST['password'];
$query = "select * from tbladmin where admin_usr_name=:userName and admin_pwd=:passWord";

try {   
  $db = new PDO($dsn, $un, $pw);
} catch (PDOException $e) {
  echo 'Connection failed: ' . $e->getMessage();
}

$statement = $db->prepare($query);

//:userName and :passWord is set when actually executing the query
$statement->execute(array(':userName'=>$username, ':passWord'=> $password)); 
$row = $statement->fetch();
?>

Notice that fetch() will only return one row (next row in resultset). If you want to access the whole recordset you can do this in 2 ways:

  1. Loop through resultset with while() { } OR
  2. Use $rows = $statement->fetchAll(); and loop through the array $rows

Use option 1 together with larger resultsets.

Use option 2 with minor resultsets (and you know it will not grow so much)

Take in consideration whether or not you should set attribute of PDO::ATTR_EMULATE_PREPARES. More info about this here: PDO MySQL: Use PDO::ATTR_EMULATE_PREPARES or not?

Community
  • 1
  • 1
bestprogrammerintheworld
  • 5,417
  • 7
  • 43
  • 72