1
$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?

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • 1
    You can use `LEFT()` in SQL. – Dharman Nov 10 '21 at 20:30
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Nov 10 '21 at 20:31

2 Answers2

0
$options_available - substr($options_available,0,2);

This should limit the string to the first 3 characters

-1

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 ...
Dylan Reimerink
  • 5,874
  • 2
  • 15
  • 21