我想请求帮助将我的mysql_ *查询转换为使用PDO技术的预准备语句。有很多我在互联网上找不到如何正确解决它们 - 例如大多数先进的,例如:
mysql_query("SELECT * FROM pet_auction JOIN people ON (pet_auction.pet=people.guid)
LEFT OUTER JOIN login.account ON (pet_auction.winner=login.account.id)
WHERE active=1 AND seller=$userid ORDER BY id DESC");
如何使用这些成功将其转换为PDO STMT?:
$people = new PDO("mysql:host=localhost;dbname=people", "myuser", "mypass");
$login = new PDO("mysql:host=localhost;dbname=login", "myuser", "mypass");
谢谢大家我宁愿不尝试其他它会是假的,因为我已经测试过......我不知道如何将LEFT OUTER JOIN
和多个数据库一起转换。
答案 0 :(得分:2)
您不需要为每个数据库打开一个pdo对象。只需授予myuser grant访问登录和人员数据库的权限。然后像这样查询:
$dbh = new PDO("mysql:host=localhost;dbname=people", "myuser", "mypass");
$stmt= $dbh->prepare("SELECT * FROM pet_auction JOIN people ON (pet_auction.pet=people.guid)
LEFT OUTER JOIN login.account ON (pet_auction.winner=login.account.id)
WHERE active=1 AND seller=:userid ORDER BY id DESC");
$stmt-> execute(array(':userid' => $userid));
$variable = $stmt->fetch(PDO::FETCH_ASSOC);
答案 1 :(得分:0)
$dbh = new PDO($dsn, "myuser", "mypass");
$select = $dbh->prepare("SELECT * FROM `table`");
$select -> execute();
$variable = $select->fetch(PDO::FETCH_ASSOC);
其中$ dsn是一个包含'mysql:dbname = racerost_reekris_db; host = localhost'的字符串
查询可以包含任何包含联接的mySQL查询。