Okai,所以我尝试使用 PDO 从 MySQL 数据库中获取多个变量,我觉得我必须在代码中重复很多次。有没有更简洁的方法来写这个或更安全的方法?
这是以下示例的代码:
$username = $_SESSION['username'];
$db = new PDO('mysql:xxxxxxxx;dbname=xxxxxxxxxxxx', 'xxxxxx', 'xxxxxxx');
// FETCH name VARIABLE
$fetchname = $db->prepare("SELECT name FROM login WHERE username = :username");
$fetchname->bindParam(':username', $username, PDO::PARAM_STR, 40);
$fetchname->execute();
$myname = $fetchname->fetchColumn();
// FETCH age VARIABLE
$fetchage = $db->prepare("SELECT age FROM login WHERE username = :username");
$fetchage->bindParam(':username', $username, PDO::PARAM_STR, 40);
$fetchage->execute();
$myage = $fetchage->fetchColumn();
我希望避免为同一张表中的每个变量重复此 FETCH ...