This question is more on this post. So I want to retrieve the number in a certain column. So let’s say the table looks like this:
A | B | C | D | E | F
---------------------
0 | 50| 0 | 0 | 0 | 0
Let’s say the user enters in B, I want to go fetch the current number in the table, which is 50. This is my SQL code:
$link = new PDO('**;dbname=**;charset=UTF-8','**','**');
$stmt = $link->prepare("SELECT MAX(:type) as max FROM table");
$stmt->bindParam(':type', $type);
$stmt->execute();
$used = $stmt->fetch(PDO::FETCH_ASSOC);
$used = $used["max"];
But this is just returning B rather than the number. How do I change my code to get the number? Thanks
Assuming
:typegets set to “B”, this will be executed as:The MAX of something with just one value will be that one value, i.e.:
Most likely you want to select the MAX from that column, not the string:
Which you can do like this:
Obviously, this is vulnerable to SQL injection if
$typecomes from a user, but if you’re getting a table name from a user, then you’re doing something wrong anyway.It’s also possible that your table would make more sense like this:
And then you could get it with: