\$\begingroup\$
\$\endgroup\$
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!
user1286956user1286956
1 Answer 1
\$\begingroup\$
\$\endgroup\$
1
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
-
\$\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– user12869562019年01月10日 14:50:40 +00:00Commented Jan 10, 2019 at 14:50
lang-php