0

I need to retrieve a string from a query and then use that later on in another SQL query.

This is what I have, I'm retrieving the current branch ID for the currently logged in user:

$neededBranch = mysqli_query($con, "SELECT BranchNumber FROM Staff WHERE staffID = '".$_SESSION['username_login']."'");

And then I need to use said string here like this:

$result = mysqli_query($con, "INSERT INTO SomeTable (
                    blah,
                    blah,
                    )
                    VALUES ('".$SomeValue."',"
                              . " '".$_SESSION['username_login']."',"
                              . " '".$neededBranch."')");

Now, the ". " '".$neededBranch."')");" does not work because it is expecting a string.

My question is: How do I actually get the value I'm needing from the first query and use it in the second query? I'm new at PHP and don't have a clue.

1
  • 1
    from what I see, the first query is correct but it is not what you want to do. To actually get the string, you can either use mysqli fetch assoc or mysql fetch array and get the value from your query. Moreover, on the second query, after "INSERT INTO SomeTable (blah, blah, )" you have to insert a third parameter (the third coloumn) in order to insert the "$neededBranch". Commented May 4, 2014 at 21:25

1 Answer 1

1

Fetch the data you queried:

$result= mysqli_query($con, $query);//query is the select stmt
$row = mysqli_fetch_assoc($result);
$neededBranch = $row['BranchNumber'];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.