'How to select first 3 character from array in $row in mysqli_fetch_assoc in while loop?
$options_economy = "SELECT * FROM options WHERE question_id='$quest_id'";
$run_opt_economy = mysqli_query($conn, $options_economy);
while ($row2 = mysqli_fetch_assoc($run_opt_economy)) {
$options_available = $row2['options'];
}
Here is my code. Many options with characters are coming in $options_available. I only want to select first 3 characters. How can I do That?
Solution 1:[1]
$options_available - substr($options_available,0,2);
This should limit the string to the first 3 characters
Solution 2:[2]
You can do it in PHP using substr for example:
$options_available = substr($options_available, 0, 3);
You can also do this in the database using the SUBSTR function:
SELECT SUBSTR(options, 0, 3) as options FROM options ...
or LEFT
SELECT LEFT(options, 3) as options FROM options ...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Felipe Fernández Sánchez |
| Solution 2 |
