0

I was trying to fetch blob image from database after trying a lot and doing many changes in code I cant resolve this error. I have bold that line in my code. If I remove that tag code works but with that tag I get an error. Why this error is occuring and what is the solution. //error Parse error: syntax error, unexpected 'data' (T_STRING), expecting ',' or ';' in C:\xampp\htdocs\project\DBMS\index.php on line 18

<html>
<?php

require "connect.php";

  $sql="select * from newbook";
$row = $conn->query($sql);
$n=0;
echo"<form method='post'><table border='0' align='center'><tr>";
    while($arr = $row->fetch_assoc()) 
    {
           $i=$arr['BookId'];
    if($n%4==0)
    {
    echo "<tr>";
    }
   echo "
    **<td height='280' width='240' align='center'><img src="data:image/jpeg;base64,'.base64_encode( $arr['BookImage'] ).'"height='200' width='200'><br/>**
    <b>BOOKNAME:</b>".$arr['BOOKNAME'].
   "<br><b>Author:</b>".$arr['Author'].
   "<br><b>Publication:</b>&nbsp;".$arr['PublicatonHouse'].
   "<br><b>Discount:</b>".$arr['Discount']."%".
   "<br><br><img src='images/MetalPlakDa5new.gif' width='70' height='20'/></a>
   <img src='images/view7.jpg' width='70' height='20'/></a>
   </td>";
  $n++;
    }




      echo "</tr></table>
       </form>";
    ?>

</body>
</html>

2 Answers2

2

Well, your quotes are all in the wrong order, do it's trying to parse text as PHP.

<html>
<?php

require "connect.php";

  $sql="select * from newbook";
$row = $conn->query($sql);
$n=0;
echo"<form method='post'><table border='0' align='center'><tr>";
    while($arr = $row->fetch_assoc()) 
    {
           $i=$arr['BookId'];
    if($n%4==0)
    {
    echo "<tr>";
    }
   echo "
    **<td height='280' width='240' align='center'><img src='data:image/jpeg;base64,".base64_encode( $arr['BookImage'] )."'height='200' width='200'><br/>**
    <b>BOOKNAME:</b>".$arr['BOOKNAME']."
   <br><b>Author:</b>".$arr['Author']."
   <br><b>Publication:</b>&nbsp;".$arr['PublicatonHouse']."
   <br><b>Discount:</b>".$arr['Discount']."%
   <br><br><img src='images/MetalPlakDa5new.gif' width='70' height='20'/></a>
   <img src='images/view7.jpg' width='70' height='20'/></a>
   </td>";
  $n++;
    }




      echo "</tr></table>
       </form>";
    ?>

</body>
</html>
Kenyon
  • 807
  • 1
  • 12
  • 24
  • Thank You it works – Abhishek Dudhal Mar 31 '16 at 17:53
  • Awesome, glad you got it working. – Kenyon Mar 31 '16 at 17:53
  • But why the order does matter that I dont get – Abhishek Dudhal Mar 31 '16 at 17:55
  • Because you open & close the string and allow PHP when you close it. For example `echo 'test';` I opened the string, and closed it with 2 single quotes. I could do `echo "test";` and for this example, it would mean the same thing. It wouldn't logically make sense to do `echo 'test";` since they technically mean different things. So if you need single quotes in your string, do `echo "someone's";` because `echo 'someone's';` has 3 single quotes, which means there is no end, and PHP thinks everything after the 3rd one is a string until it finds another matching one - causing a parse error – Kenyon Mar 31 '16 at 17:59
  • Great explaination thank you – Abhishek Dudhal Mar 31 '16 at 18:01
  • No problem, glad it makes sense! – Kenyon Mar 31 '16 at 18:03
1
echo "
    **<td height='280' width='240' align='center'><img src='data:image/jpeg;base64,".base64_encode( $arr['BookImage'] )."'height='200' width='200'><br/>**

You issue is with the Double brackets before data, use single quotes instead and end it with single quotes. Try the above. Your issue has nothing to do with the BLOB.

Parthapratim Neog
  • 4,352
  • 6
  • 43
  • 79
  • No same error still occur – Abhishek Dudhal Mar 31 '16 at 17:51
  • That is the issue, it was just in more places. Check my answer. – Kenyon Mar 31 '16 at 17:52
  • 1
    Fix your quotes. Its all messed up. Nothing else is wrong here. And yah @Kenyon already gave a proper answer. But I suggest you be carefull when echoing and concating string with variables, or php codes in between. – Parthapratim Neog Mar 31 '16 at 17:54
  • 1
    To expound what Parthapratim Neog is saying: make sure you aren't echo'ing out unsanitized strings, otherwise you could end up with an XSS vulnerability. You should be escaping HTML in some way or another whenever you output text that could have come from a user or 3rd party – Kenyon Mar 31 '16 at 17:56