0

I had a very random problem in an IIS class, it has stumped my tutor so here I am and I'll try my best to explain it well!

I'm running Xampp with Apache and MySQL, I run the query I want and get the expected output to a table, but I have a problem with the outputting of pictures. I have the right file type, extention and path selected, because I can get pictures to show up, but as long as at least one picture in the query result has the extension removed, and this picture will not load.

Database

enter image description here

Website

enter image description here

If I have each query result with the correct name and extension, which is the same as when it shows up, none of them show up at all!

PHP:

<?php 

// set server access variables 
  include 'db2.inc';

// open connection 
$connection = mysql_connect($hostname, $username, $password) or die ("Unable             to connect!"); 

// select database 
mysql_select_db($databaseName) or die ("Unable to select database!"); 

// create query 
//$query = "SELECT * FROM products"; 
$query = "SELECT * FROM products WHERE CategoryName = 'Surfboards'"; 
//Check initial letter
//if (!$initialLetter=="")
//{ 
//  $query = $query." Where country like  '$initialLetter%' ";
//}

// execute query 
$result = mysql_query($query) or die ("Error in query: $query.     ".mysql_error()); 

// see if any rows were returned 
if (mysql_num_rows($result) > 0) { 
// yes 
// print them one after another 
echo "<table cellpadding=20 border=1>"; 
while($row = mysql_fetch_assoc($result)) { 
    echo "<tr>"; 
    echo "<td>".$row['ProductID']."</td>"; 
    echo "<td>".$row['Name']."</td>"; 
    echo "<td>".$row['Description']."</td>"; 
    echo "<td>".$row['Brand']."</td>"; 
    echo "<td>".$row['Model']."</td>"; 
    echo "<td>".$row['BoardLength']."</td>"; 
    echo "<td>".$row['BoardType']."</td>"; 
    echo "<td>".$row['Colour']."</td>"; 
    echo "<td>"."<img src=images/".$row['Image']."> </td>";
    echo "<td>".$row['UnitPrice']."</td>"; 
    echo "<td>".$row['CategoryName']."</td>"; 
    echo "</tr>"; 
} 
echo "</table>"; 
} 
else { 
// no 
// print status message 
echo "No rows found!"; 
} 

// free result set memory 
mysql_free_result($result); 

// close connection 
mysql_close($connection); 

?> 

Any help or direction would be greatly appreciated!

Thanks

Will

Junius L.
  • 13,163
  • 2
  • 28
  • 58
Will
  • 29
  • 1
  • So what do your http servers log files tell you about those failing requests? – arkascha May 02 '17 at 20:17
  • I could imagine that some rewriting rules kick in here, but with that little details you posted this is guess work. – arkascha May 02 '17 at 20:18
  • 2
    **WARNING**: If you're just learning PHP, please, do not learn the obsolete [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It's awful and has been removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) helps explain best practices. Make **sure** your user parameters are [properly escaped](http://bobby-tables.com/php) or you will end up with severe [SQL injection bugs](http://bobby-tables.com/). – tadman May 02 '17 at 20:23
  • 2
    If they're teaching you `mysql_query` in school, that's utterly shameful and they should update their curriculum to something from the 21st century. – tadman May 02 '17 at 20:24
  • Check your browser console for errors related to the image URLs. – Barmar May 02 '17 at 20:34
  • @tadman it's very common for educational establishments to be upto 10 years behind current best practise. This was true when I was doing Software Engineering ten years ago, and from viewing the students on SO it's still true today, seemlingly for both UK and US educators – Martin May 02 '17 at 20:37
  • [This post should help you find your issue](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – Martin May 02 '17 at 20:42
  • 1
    @Martin Keep in mind ten years behind is 2007. This is code from 1997 back when computers had megabytes of memory and PHP 3 was still a thing. – tadman May 02 '17 at 20:44
  • @tadman ah you know what they say, *"time flies when you're answering questions on SO"* . But yeah, the way education establishments work is very, very slow, as with other topics such as business or geography or even medicine, the basics just don't change often enough, and people who decide an establishment should teach IT don't seem to realise that it's a constantly reinvention process, that almost all other topics simply don't have, so it takes time **and money** for places such as schools and universities to learn new things to teach, it sucks but that's why Will is learning `mysql_` `:-(` – Martin May 02 '17 at 20:48
  • Thanks for the comments guys, back to the drawing board and I'll have another go with up to date PHP! – Will May 03 '17 at 21:09

1 Answers1

1

On this line:

echo "<td>"."<img src=images/".$row['Image']."> </td>";

Your image src is not enclosed in quotes. If this is a direct copy/paste of your code then that is definitely a problem, but may not be the only one.

As other people have said, look at pdo, or mysqli. The mysql_ functions you are using are deprecated for multiple reasons.

DewiW
  • 108
  • 3
  • 1
    As long as the URL doesn't contain any special characters, it should work without quotes, although quotes are preferred. – Barmar May 02 '17 at 20:32
  • I actually didn't know that. Would / be a special character though? – DewiW May 02 '17 at 21:10
  • No. I've seen lots of HTML code that has unquoted URLs, the `/` doesn't cause a problem. Try it and see. Delimiters are space, quotes, and `>`. – Barmar May 02 '17 at 21:17