I have a question. My friend told me PHP is better option but I'm not sure, I think sql might be faster. So basically I'm trying to get 50% of my db record and I use mysql function substr to do that. Of course I can do the same thing in PHP after data fetching, using this function right bellow.
sql
"select *,substr(text,1,(CHAR_LENGTH(text)*0.5)) as text FROM stories"
php
$data=$sql->fetchAll(PDO::FETCH_ASSOC);
$length=count($data);
for($i=0;$i<$length;$i++){
$data[$i]['text']=substr($data[$i]['text'],0,strlen($data[$i]['text'])*0.5);}
What should I use and why? thank you
select *, ..you are fetching all data includingtext+ the half of thetext.PDO::FETCH_ASSOCPHP will overwrite the first columntext(full data) with the second one (half data). But before that, both columns are returned from the DB to PHP. So actually you are fetching 50% more data instead of 50% less. As Gordon wrote in his answer, you should only select columns that you are going to use.