please hellp

Discussion in 'MySQL' started by david varen, Apr 26, 2020.

  1. #2
    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?
     
    david varen, Apr 26, 2020 IP
  2. seomanualsubmission

    seomanualsubmission Well-Known Member

    Messages:
    931
    Likes Received:
    133
    Best Answers:
    4
    Trophy Points:
    165
    #3
    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.
     
    seomanualsubmission, Apr 26, 2020 IP
  3. david varen

    david varen Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #4
    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();
     
    david varen, Apr 26, 2020 IP
  4. seomanualsubmission

    seomanualsubmission Well-Known Member

    Messages:
    931
    Likes Received:
    133
    Best Answers:
    4
    Trophy Points:
    165
    #5
    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
     
    seomanualsubmission, Apr 26, 2020 IP
  5. david varen

    david varen Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    nothing helps.
    I'm desperate .
     
    david varen, Apr 26, 2020 IP