I need to do the action "SELECT transferid,SUM(despoit-transferAmount) AS money from money_transfer" WHERE userid=$_SESSION[userid]. the userid is in the users table. how do I combined the two tables so the action will work?
Why not you are fetching first users table if its in table. $select = SELECT userid from users WHERE username="name" OR Other=othercondition LIMIT 1 Then fetch it $row = mysqli_fetch_assoc ($select); $_SESSION[userid] = $row["userid"]; (No Need to do this) because you can put $row["userid"] directly in condition. Then process your statement SELECT transferid,SUM(despoit-transferAmount) AS money from money_transfer" WHERE userid=$_SESSION[userid] (or userid=$row["userid"]) I think its very simple ..... Or you have describe a bit more about your code because according to your details its very simple for me because users table is already in database so can fetch very easily.
this is how I write the code but it dosent work: $mysqli = new mysqli("localhost","root",'',"bank"); if ($mysqli -> connect_errno) { echo "Failed to connect to MySQL: " . $mysqli -> connect_error; exit(); } $select = "SELECT userid from users WHERE userid=$_SESSION[userid] LIMIT 1"; $row = mysqli_fetch_assoc ($select); $sql= "SELECT transferid,SUM(despoit-transferAmount) AS money from money_transfer WHERE userid=$_SESSION[userid] )" if ($result = $mysqli -> query($sql)) { while ($obj = $result -> fetch_object()) { printf($obj->money."$"); } $result -> free_result(); } $mysqli -> close();
Why are you fetching data from users table while using $_SESSION["userid"] in both statement. Your first statement to get record from users are completely useless. And because you are fetching two values from money_transfer table so need to define it that what you want to print there. Take a look at www. w3schools. com/pHp/func_mysqli_free_result.asp