简体   繁体   English

存储来自 SQL 表的变量,以便稍后使用 PHP

[英]Store variable from SQL Table to use later using PHP

I would like to be able to store a variable from a column to use to update a variable in another column.我希望能够存储列中的变量以用于更新另一列中的变量。

Here is my code:这是我的代码:

        $sqlPlaceNumber = "SELECT @locationPlace := `$locationPlace` FROM `$className` WHERE `Vehicle` = `$vehicleName` ";
        $results = mysqli_query($con, $sqlPlaceNumber) or die(mysqli_error($con));

        $sqlUpdate = "UPDATE `$className` SET `$location` = 31-`@locationPlace` WHERE Vehicle = `$vehicleName`";
        mysqli_query($con, $sqlUpdate) or die(mysqli_error($con));

这是我的桌子

I would like to store the value of column "Colby, WI Place" to use to subtract it from 31 to get the value for column "Colby, WI"我想存储“Colby, WI Place”列的值,用于从 31 中减去它以获得“Colby, WI”列的值

31-"Colby, WI Place" = new value for "Colby, WI" 31-"Colby, WI Place" = "Colby, WI" 的新值

It looks like you want:看起来你想要:

update mytable set `Colby, WI` = 31 - `Colby, WI Place` where vehicle = ?

There is no need to use an intermediate variable, you can do this what you want with a single update statement.无需使用中间变量,您可以使用单个update语句执行此操作。

Also I would strongly recommend using column names that do not contain special characters (they should be made of alphanumeric characters and underscores only), so you don't need to worry about quoting them.此外,我强烈建议使用不包含特殊字符的列名(它们应仅由字母数字字符和下划线组成),因此您不必担心引用它们。

Finally: you do want to use parameterized queries to make your code safer and more efficient.最后:您确实希望使用参数化查询来使您的代码更安全、更高效。 You can have a look at this post for more information.您可以查看这篇文章以获取更多信息。

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

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