images from database column not loading

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Netroxy
Forum Commoner
Posts: 29
Joined: Sat Apr 09, 2011 5:38 pm

images from database column not loading

Post by Netroxy »

<?php
$page = $_GET['page'];
$category = $_GET['cat'];
$your_db = @ new mysqli("database","name","password");
if (mysqli_connect_errno())
{
echo 'ERROR!<br />'.mysqli_connect_errno()
.' - Not connected : '.mysqli_connect_error().'<br />';
die;
}
else
{
$db_connect = $your_db->select_db("databasename");
if (!$db_connect)
{
echo 'ERROR CONNECT DATA BASE';
die;
}
}
echo '<p>';
$query = "select distinct fldCity from info order by fldCity";
$result = $your_db->query($query);
$number_of_records = $result->num_rows;
for ($i = 0; $i < $number_of_records; $i++)
{
$row = $result->fetch_assoc();
echo '<a href="index.php?cat='.stripslashes($row['fldCity']).'">';
echo stripslashes($row['fldCity']);
echo '</a>&nbsp;/&nbsp;';
}
echo '</p>';
if ($category)
{
$query = "select fldCompanyLogo, fldCompanyName, fldAddress, fldCity, fldProvince, fldPostalCode, fldMenuLink
from info where fldCity = '$category' and length(fldCompanyLogo) > 0 order by fldCity";
}
else
{
$query = "select fldCompanyLogo, fldCompanyName, fldAddress, fldCity, fldProvince, fldPostalCode, fldMenuLink
from info where length(fldCompanyLogo) > 0 order by fldCity";
}

$result = $your_db->query($query);
$number_of_records = $result->num_rows;
$num_pages = $number_of_records / 7;
if (($number_of_records % 7) > 0 )
{
$num_pages++;
}
if (strlen($page) == 0)
{
$page = 0;
}
else
{
$page = $page * 4;
}
echo '<table border="0" cellspacing="10" cellpadding="10" style="border-collapse: collapse" bordercolor="#111111">';
$row_num = 4;
$result->data_seek($page);
for ($i = $page; $i < $number_of_records; $i++)
{
if ($row_num <= 4)
{
echo '<tr>';
for ($col_num = 0; $col_num < 4; $col_num++)
{
$row = $result->fetch_assoc();
echo '<td>';
show_image(stripslashes($row['fldCompanyLogo']),stripslashes($row['fldCompanyName']));
echo '<br />';
echo '<b><font face="Tahoma" size="2"><a href="mysite.ca/'.stripslashes($row['fldMenuLink']).'" target="_blank"></font>';
echo '<font face="Tahoma" size="2"><a href="'.stripslashes($row['fldMenuLink']).'" target="_blank">';
echo stripslashes($row['fldCompanyName']);
echo '</a>';
echo '<br />';
echo stripslashes($row['fldCity']);
echo '<br />';
echo stripslashes($row['fldProvince']);
echo '<br />';
echo stripslashes($row['fldPostalCode']);
echo '</td>';
}
$row_num++;
echo '</tr>';
}
else
{
break;
}
}
echo '</table>';
for ($j = 0; $j < $num_pages; $j++)
{
$page_link = $j + 1;
echo '<font face="Tahoma" size="4"><b><a href="index.php?page='.$j.'&cat='.$category.'">'.$page_link.'</a></b></font>&nbsp;';
}
echo '&nbsp;'.$number_of_records;
function show_image($image_name)
{
if (file_exists("'$image_name'"))
{
$dim_img = getimagesize('/images/'.$image_name);
echo '<img src="/images/'.$image_name.'" alt = '.$alt.' border=0 align="bottom"';
echo 'width = '. $dim_img[100] .' height = ' .$dim_img[100] . ' />';
}
else
echo 'Add your image here!';

}
?>


==============================================================

I am totally lost and I can't find the error to why the images from the database isn't loading with the <img src=""> script. I highlighted the area in blue where I believe the error is to be. I just can't find any errors in that particular spot! All I want is the images from a column I have in my database to be fetched and connected to 'echo '<img src="/images/'.$image_name.'" alt = '.$alt.' border=0 align="bottom"';' as you can see in the last few codes. As you can see, .$image_name. is connected to the show_image function but doesnt seem to view the images. Im totally lost. $image_name basically connects 'image.jpg', from the database to '<img src="/images/'', thereby viewing the jpg image in the pagination script. 'if (file_exists("'$image_name'"))' isnt connecting to show_image function as well.

No images are being displayed, and I can't find the error as to why the images aren't showing up :?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: images from database column not loading

Post by requinix »

Try with is_file() instead of file_exists(). And it'd be a good idea to check if the $image_name isn't empty before you use it for anything.

Otherwise what HTML does the script output?
Netroxy
Forum Commoner
Posts: 29
Joined: Sat Apr 09, 2011 5:38 pm

Re: images from database column not loading

Post by Netroxy »

When I execute the page, it doesn't even show <img src="">. I tried is_file() and it still shows blank images. All I am receiving is "Add your image here!".
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: images from database column not loading

Post by requinix »

Allow me to repost part of your code but this time with proper BBCode tags for code.

Code: Select all

function show_image($image_name)
{
if (file_exists("'$image_name'"))
{
$dim_img = getimagesize('/images/'.$image_name);
echo '<img src="/images/'.$image_name.'" alt = '.$alt.' border=0 align="bottom"';
echo 'width = '. $dim_img[100] .' height = ' .$dim_img[100] . ' />';
}
else
echo 'Add your image here!';
}
Now take a close look at the file_exists().
Netroxy
Forum Commoner
Posts: 29
Joined: Sat Apr 09, 2011 5:38 pm

Re: images from database column not loading

Post by Netroxy »

i switched it up with is_file() and for some reason it worked. whats the difference between is_file() and file_exists()? do they have similar functions?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: images from database column not loading

Post by requinix »

is_file() does exactly what it sounds like it does. file_exists() tells you if the thing exists as a file or as a directory.

Your problem is that $image_name is empty. It shouldn't be empty. You need to fix that. When it's empty you're testing for "/images/", which most certainly exists. But it's a directory...
Netroxy
Forum Commoner
Posts: 29
Joined: Sat Apr 09, 2011 5:38 pm

Re: images from database column not loading

Post by Netroxy »

Thanks for the help. I solved the $image_name
Post Reply