Categories: php, mysql, sql, database

Store variable from SQL Table to use later using PHP

1 answer

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)); 

Here is my table

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"

31-"Colby, WI Place" = new value for "Colby, WI"

All answers to this question, which has the identifier 61238463

The best answer:

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.

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.

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type is referenced as an interface from”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data