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!

asked Jan 10, 2019 at 14:13
\$\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;
answered Jan 10, 2019 at 14:31
\$\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

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.