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\$
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\$
  • \$\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\$ – user1286956 Jan 10 at 14:50

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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