hello trying convert code old msql pdo can upload , view images database, following error code
notice: array string conversion in c:\xampp\htdocs\pentaslash\includes\addpost.php on line 60
if change fetchall fetch not identify row[2] database has following rows id (pk) name (imagename) image(longblob)
<html> <body> <form method="post" enctype="multipart/form-data"> <br/> <input type="file" name="image" /> <br/><br/> <input type="submit" name="submit" value="upload"/> </form> <?php if(isset($_post['submit'])) { if(getimagesize($_files['image']['tmp_name']) == false) { echo "please select image"; } else { $image= addslashes($_files['image']['tmp_name']); $name= addslashes($_files['image']['name']); $image= file_get_contents($image); $image= base64_encode($image); saveimage($name,$image); } } displayimage(); function saveimage($name,$image) { try { define('site_root', dirname(__file__)); require site_root . '\dbconnect.php'; $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception); // <== add line $sql="insert images (name,image) values ('$name','$image')"; if ($conn->query($sql)) { echo "uplaoded"; } else{ echo "failed"; } $conn = null; } catch(pdoexception $e) { echo $e->getmessage(); } } function displayimage() { try { require site_root . '\dbconnect.php'; $conn->setattribute(pdo::attr_errmode, pdo::errmode_exception); $result = $conn->prepare("select * images"); $result->execute(); while ($row = $result->fetchall(pdo::fetch_assoc)) { echo '<img height="300" width="300" scr="data:image;base64,' .$row[2]. '">'; } $conn = null; } catch(pdoexception $e) { echo $e->getmessage(); } } ?> </body> </html>
using fetchall() , foreach loop, can try this:
$rows = $result->fetchall(); foreach($rows $row): echo '<img height="300" width="300" scr="data:image;base64,' .$row['image']. '">'; endforeach;
if still want use while loop, should be:
while($rows = $result->fetch(pdo::fetch_assoc)){ echo '<img height="300" width="300" scr="data:image;base64,' .$row['image']. '">'; }
Comments
Post a Comment