0
\$\begingroup\$

I joined this 2 tables and dropped the user_id afterwards. I am defining the user_id elsewhere in my code based on my session/cookies and I do not want to return this value as a global. Is there a more compact or elegant way to write this:

public function test($user_id)
{
    $stmt = $this->conn->prepare("CREATE TEMPORARY TABLE `temp_tb`
                                  SELECT a.*, b.token
                                  FROM $this->tableName a
                                  LEFT JOIN `requests` b
                                  ON a.$this->user_id = b.uid
                                  WHERE $this->user_id=:user_id ");
    $stmt->execute(array(":user_id"=>$user_id));
    $stmt = $this->conn->prepare("ALTER TABLE `temp_tb` DROP $this->user_id ");
    $stmt->execute();
    $stmt = $this->conn->prepare("SELECT * FROM `temp_tb` ");
    $stmt->execute();
    return $stmt->fetch(PDO::FETCH_ASSOC);
}

Thank you!

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Well, I hardly understand the whole affair but from what I can get about it

$sql = "SELECT a.*, b.token FROM $this->tableName a
        LEFT JOIN `requests` b ON a.$this->user_id = b.uid
        WHERE $this->user_id=:user_id";
$this->conn->prepare($sql);
$stmt->execute(array(":user_id"=>$user_id));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
unset($row[$this->user_id]);
return $row;
\$\endgroup\$
1
  • \$\begingroup\$ This is a testing ground for me. I am defining the Cookie to regenerate each time someone logs in with it, plus the cookies have to match the IP it was initially created on. Never the less, I can also define this through the session to tell the database which user_id to fetch. The test function only gets some basic information about the user that will be used to populate the index page after the login has taken place. \$\endgroup\$ Commented Jan 10, 2019 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.