2

When i used the LEFT() to fetch the values from database using the following code

$select="SELECT LEFT(description,500) FROM tbl_news where id='$id'";
$quer=mysqli_query($select);
$fetch=mysqli_fetch_array($quer);
$descr=$fetch['description'];

echo $descr;

The value is not echoing... Is the LEFT() didn't work inside while loop??

John Woo
  • 258,903
  • 69
  • 498
  • 492
Anoop
  • 252
  • 1
  • 4
  • 12
  • 2
    Apart from below answer you can also try `echo $fetch['LEFT(description,500)'];` . Just another way ;) – Rikesh Jan 30 '13 at 09:33

1 Answers1

3

supply an ALIAS on the column pass on the function.

$select="SELECT LEFT(description,500) description FROM tbl_news where id='$id'";

As a sidenote, the query is vulnerable with SQL Injection if the value(s) of the variables came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492