如何将旧的mysql转换为pdo

时间:2013-01-21 10:51:57

标签: php mysql

我正在尝试将我的mysql代码转换为新的pdo代码我真的需要一些帮助,因为mysql被折旧了。我是php和mysql的新手,需要一个很好的例子来继续。谢谢你的帮助。

旧MSYQL

<?php
include_once("Connections/database.php");
$login = $_SESSION['login'];

$query = mysql_query("SELECT * FROM proryniti_members WHERE email='".$login."'");
if (mysql_num_rows($query)==0)
    die("member not found!");
else
{
    $row = mysql_fetch_assoc($query);
    $firstName = $row['firstName'];
    $lastName = $row['lastName'];
}

?>

PDO代码

<?php
include_once("Connections/database.php");
$login = $_SESSION['login'];

$qry = $dbh->prepare(
  'SELECT * FROM proryniti_members WHERE email = :'.$login.'');
$qry->execute(array($login));
$row = $qry->fetch();
if (!is_null($row)) {
    $firstName = $row['firstName'];
    $lastName = $row['lastName'];
} 
?>

1 个答案:

答案 0 :(得分:7)

您不明白准备好的查询在PDO中的工作方式。我建议你回去阅读网上的文档或示例。我在上面修改了你的代码,看看第6-5行:

 <?php
   include_once("Connections/database.php");
  $login = $_SESSION['login'];

  $qry = $dbh->prepare('SELECT * FROM proryniti_members WHERE email=:login');
  $qry->execute(array(':login'=>$login));
  $row = $qry->fetch();
  if (!is_null($row)) {
  $firstName = $row['firstName'];
  $lastName = $row['lastName'];
  } 
  ?>