简体   繁体   English

PHP-从MySQL查询获取字符串

[英]PHP - Getting a string from MySQL Query

I need to retrieve a string from a query and then use that later on in another SQL query. 我需要从查询中检索一个字符串,然后在另一个SQL查询中使用它。

This is what I have, I'm retrieving the current branch ID for the currently logged in user: 这就是我所拥有的,我正在为当前登录用户检索当前分支ID:

$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. 现在,“ . " '".$neededBranch."')"); “不起作用,因为它需要一个字符串。

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. 我是PHP的新手,没有头绪。

Fetch the data you queried: 获取您查询的数据:

$result= mysqli_query($con, $query);//query is the select stmt
$row = mysqli_fetch_assoc($result);
$neededBranch = $row['BranchNumber'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM