Image from MYSQL database (PHP, MySql) - php

I am trying to display a image URL from mysql database into my website. It's saved as URL in my database.
http://www.upload.ee/image/5697422/prague-1168302_1920.jpg
this is the url which should appear as $rida['pilt']
<?php
$paring = 'SELECT * FROM postitus ORDER BY id';
$valjund = $yhendus->query($paring);
while($rida = mysqli_fetch_assoc($valjund))
?>
<img class="img-thumbnail" alt="city" src="<?php echo $rida['pilt']; ?>" style="width:250px;height:200px" />

I guess in your code you're getting error undefined index as you're accessing your variable out of while loop. Try below code:
<?php
$paring = 'SELECT * FROM postitus ORDER BY id';
$valjund = $yhendus->query($paring);
while($rida = mysqli_fetch_assoc($valjund)) : ?>
<img class='img-thumbnail' alt='city' src='<?php echo $rida['pilt']; ?>' style="width:250px;height:200px" />
<?php endwhile; ?>

You should recheck $rida. After you use "echo var_dump($rida);", you will know all parameters in array. You can choose trust array

Related

FuelPHP- Using Asset::img() with images fetched from database by for loop

<?php foreach($works as $work) : ?>
<?php echo Asset::img('project-icons/icon/$work->cover_img', array('class'=>'img-responsive', 'alt'=>'...')); ?>
<?php endforeach; ?>
I am using FuelPHP to build a portfolio website. PHP v5.6
This is the "works" section where images and details of a "work" are fetched from database. Using foreach loop. I want to get the "cover_img" (which is the image name), inside the Asset::img(..)
How to do this? what to put in place of $work->cover_img?
Edit: I was able to get the result by using this:
<img src="<?php echo Uri::base(false); ?>/assets/img/project-icons/icon/<?php echo $work->cover_url; ?>" class="img-responsive" alt="..." />
Can this be achieved by using Asset::img() instead of Uri::base()?
Can't you just do:
<?php
echo Asset::img('project-icons/icon/'.$work->cover_img, array('class'=>'img-responsive', 'alt'=>'...'));
?>
...?

Using if-statements in PHP to print HTML

I'm fairly new to PHP and I've been trying to construct some code to print basic HTML, however the code causes an error 500 whenever used. I am guessing it is a syntax error since I've tried the code in a couple of forms and nothing seems to work (including removing the database lookup and just trying to compare to set values to each other). The script needs to get a variable from the db, compare it to a set value and print the HTML if true, here is the code I am trying:
<?php
$db = &JFactory::getDBO();
$id = JRequest::getString('id');
$db->setQuery('SELECT #__categories.title FROM #__content, #__categories WHERE #__content.catid = #__categories.id AND #__content.id = '.$id);
$category = $db->loadResult(); ?>
<?php if strcmp($category,"Blog")==0 : ?>
<div style="display: -webkit-inline-box" class="sharelogos">
<img src="/images/sharing-icons/facebook.png" width="30px" alt="Facebook" />
</div>
<?php endif; ?>
Any help will be appreciated, thanks!
You if is incorrect, try like this
<?php if (strcmp($category,"Blog")==0) { ?>
<div style="display: -webkit-inline-box" class="sharelogos">
<img src="/images/sharing-icons/facebook.png" width="30px" alt="Facebook" />
</div>
<?php } ?>

How to Serve 4 Images (Blobs) from Mysql using PHP in a single HTTP Response?

I am trying to display 4 images using PHP and MySQL database. I have to display the 4 images as rows.
I use the table cars with fields (id_car, car_image1, car_image2, car_image3, car_image4), with all the images being blob datatype.
$id = $_GET['id'];
$link = mysql_connect("localhost", "root", "");
mysql_select_db("cars_database");
$sql = "SELECT car_image1, car_image2, car_image3, car_image4 FROM cars WHERE id_car='$id'";
$result = mysql_query("$sql");
mysql_close($link);
while($row=mysql_fetch_array($result))
{
header('Content-type: image/jpeg');
echo $row['car_image1'];
echo $row['car_image2'];
echo $row['car_image3'];
echo $row['car_image4'];
}
I can only display 1 image and not the other images. Since I am a newbie to this technology I need help.
That's how HTTP works (at least, current version): you cannot have a URL that points to more than one resource simultaneously. Your script needs to send one image.
Simply, call it four times:
<img src="/get-image.php?id=1">
<img src="/get-image.php?id=2">
<img src="/get-image.php?id=3">
<img src="/get-image.php?id=4">
You need to define path for images.
<?php
while($row=mysql_fetch_array($result))
{
header('Content-type: image/jpeg');
?>
<img src="path<?php echo $row['car_image1']; ?>" alt="" />
<img src="path<?php echo $row['car_image2']; ?>" alt="" />
<img src="path<?php echo $row['car_image3']; ?>" alt="" />
<img src="path<?php echo $row['car_image4']; ?>" alt="" />
<?php
}
?>

passing data using hyperlink in PHP

I'm trying to pass the path of an image ['guest'] from one page to another using a link. (I'm storing URLs of images in database)
Can't seem to get the image to display, which is a bigger image of 'url'. I'm doing it this way so that I can have a larger image displayed in the target page (does_this_work.php) plus adding some other bits on the page too.
I'm still learning and can;t seem to see what I'm doing wrong. Any help appreciated,
<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
// Connect to server and select databse.
mysql_connect($host, $username, $password)or die("cannot connect");
mysql_select_db($db_name) or die("cannot select DB");
$photo=mysql_query("SELECT * FROM `images` ORDER BY (ID = 11) DESC, RAND() LIMIT 7");
while($get_photo=mysql_fetch_array($photo)){ ?>
<div style="width:300px;">
<a href="does_this_work.php?big_image=$get_photo['guest']>" target=""><img src="<?
echo $get_photo['url']; ?>" title="">
</div>
<? } ?>
I then use the following code to try and display the array data in the target file
<?php
echo "this is the page where you get a larger picture of image on previous page, plus
further info";
$big_image = $_GET['guest'];
echo $big_image;
?>
You're missing an opening tag in here (And a closing semi colon, but that's not as problematic here):
<a href="does_this_work.php?big_image=$get_photo['guest'] ?>"
Change to:
<a href="does_this_work.php?big_image=<?= $get_photo['guest']; ?>"
There are several errors in your code. First of all, this is how you use $_GET and $_POST:
ex. site.php?argument=value
To retrieve the value of argument, you need this code in site.php:
//The variable must not necessarily be $value
$value = $_GET['argument'];
//Alt.
$value = $_POST['argument'];
Secondly (like the other answers tell you) you are missing a php-tag here:
<a href="does_this_work.php?big_image=$get_photo['guest']>" target=""><img src="<? echo $get_photo['url']; ?>" title="">
Instead it should be:
<a href="does_this_work.php?big_image=<?php echo $get_photo['guest']; ?>" target=""><img src="<?php echo $get_photo['url']; ?>" title="">
Now, in order to make that compatible with your second code, you need to change the argument sent to guest like this:
<a href="does_this_work.php?guest=<?php echo $get_photo['guest']; ?>" target=""><img src="<?php echo $get_photo['url']; ?>" title="">
OR change the $_GET['guest']; to $_GET['big_image'];
I think I got it all right..
<a href="does_this_work.php?big_image=$get_photo['guest'] ?>" target=""><img src="<?
echo $get_photo['url']; ?>" title="">
You are missing your php starting tags. This should read:
<a href="does_this_work.php?big_image=<? $get_photo['guest'] ?>" target=""><img src="<?
echo $get_photo['url']; ?>" title="">

Can't get image to display in dynamic pages

I can't seem to get my image to display properly. Previously, I have used the following code snippet and it worked perfectly.
catalog.php (worked perfectly):
<p class="image">
<a href="synopsis.php?id=<?php echo $row['id']; ?>">
<img src="getImage.php?id=<?php echo $row['id']; ?>" alt="" width="175" height="200" />
</a>
</p>
synopsis.php (not displaying image at all):
<?php
$id = $_GET['id'];
...?>
<p class="image">
<img border="0" class="floatleft" src="getImage.php?id=<?php echo $row['id']; ?>" width="250" height="400" />
<?php echo $row['synopsis']; ?>
</p>
where getimage.php:
<?php
$id = $_GET['id'];
$link = mysql_connect("localhost", "root", "");
mysql_select_db("dvddb", $link);
$sql = "SELECT dvdimage_path FROM dvd WHERE id=$id";
$result = mysql_query($sql, $link);
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
echo file_get_contents($row['dvdimage_path']);
?>
Any idea why can't I display this image?
EDIT 1:
So after debugging, I got an error message:
Undefined index: id in C:\xampp\htdocs\synopsis.php on line 106
so i went to add the following code into the php code just before echo $row['id']:
<p>getImage.php?id=<?php error_reporting(0); echo $row['id']; ?></p>
However,
the paragraph i got was just getImage.php?id=.
Then, i went into synopsis.php -> <img border="0" class="floatleft" src="getImage.php?id=<?php echo $row['id']; ?>
and changed that into:
<img border="0" class="floatleft" src="getImage.php?id=2">
Again, same problem happens, where i can't get the specific image out.
I suspect something is wrong with my getimage.php file. However, this getimage.php file has been working fine for other pages when i use the snippet.
My requirements are very simple:
In catalog.php, i populate images and text from dvd database using a while loop. Then, each of these images has got their specific primary ID. when i click the the images, they will go to the link: synopsis.php?id="primaryid" Then, using this "primaryid" i should be able use getimage.php?"primaryid" to generate an image on synopsis.php page.
EDIT 2:
actually, i made a syntax error somewhere. So this line:
<img border="0" class="floatleft" src="getImage.php?id=2">
is working perfectly, this means the fault lies in somewhere that i cant echo 'id' out correctly.
EDIT 3:
I have included the links to the relevant source code:
catalog.php
synopsis.php
getimage.php
sortmenu.css
style.css
database in xml format
Questions to ask yourself:
Is it really required that you use a php script to mimic the image? If not, just use the image path.
Is there any output before the header(); function in the getimage.php file? Even just a space before the
Is the image actually a JPEG?
Are there any errors coming up when you go to getimage.php?id=ID in your browser?
In synopsis.php you are getting the id from the querystring but then trying to use a database value. I cant see all of your code so im posting solutions to cover two scenarios
src="getImage.php?id=<?php echo $id; ?>"
or
src="getImage.php?id=<?php echo $row[$id]; ?>"

Categories