PHP 'IF Statement' comparing two tables in MYSQL

Posted on 2008-11-15
Last Modified: 2013-12-13
I am relatively new to PHP, I understand some and other bits confuse me... I have two tables n a database one called timages (Where I keep images) and one called technical data (where I store data)
On adding the data to the database, if there is no image for the particular data being added I auto upload a default gif image in its place.
When the data is called, the imageid in the technocaldata calls the reference for the image by id in timages.
My problem is that in technicaldata I store an imageid that relates to timages, so that I can call the technicaldata image as requested. what I need to do is have an IF Statement (think) that if the image that is called by the imageid is a gif, it doesnot display the links.
What I can do is call the timages type as stored and then checj if it is a gif and then have the IF replace the coe in the script.

I have attached the code where I call the technicaldata whee I have added to if statements to replace the links if they are needed or not. It is the val [6} in the IF staement that I need to compare to:
SELECT type FROM timages WHERE id="'.$val[6].'"
Then either display nothing if $val[6] = to a gif or if not display the two links $tdsdownload $tdslink

This is probably a bit confusing! Any help would be much a[[reciated

include 'admin/connect.php';

$res=mysql_query('SELECT id,tdstitle,subheading,supplier,dat,category,imageid FROM technicaldata WHERE category="2" ORDER BY tdstitle DESC',$dbh) or die(mysql_error());


for($run=0; $run<$num; $run++)



if ($val[6]=='0') $tdsdownload='&nbsp;'; else $tdsdownload='<a href="imagedownload.php?a='.$val[6].'"><img src="images/download.gif" alt="Download TDS" width="30" height="22" border="0"></a>';

if ($val[6]=='0') $tdslink='&nbsp;'; else $tdslink='<a href="image.php?a='.$val[6].'&TB_iframe=true&height=760&width=730" rel="sexylightbox"><img src="images/tds.gif" alt="View TDS" width="30" height="22" border="0"></a>';

echo '<table width="820" border="0" cellspacing="0" cellpadding="0" bgcolor="'.$col.'">


  <td height="40" align="right" valign="middle"><table width="820" height="22" border="0" cellspacing="0" cellpadding="0">


      <td width="303" align="left" class="mainTXT12LEFT"><a href="image.php?a='.$val[6].'&TB_iframe=true&height=760&width=730" rel="sexylightbox"><span class="Formtitles13">'.$val[1].'</span></td>

      <td width="327" align="left" class="mainTXT12LEFT">'.$val[2].'</td>

      <td width="110" height="22" align="left"><span class="mainTXT12LEFT">'.$val[4].'</span></td>

      <td width="41" align="left">'.$tdsdownload.'</td>

      <td width="39" align="center" class="maineleven_BK">'.$tdslink.'</td>





      <td height="1" align="center" valign="top"><img src="images/greenspacer.gif" width="820" height="1"></td>






Open in new window

Question by:flashjordan
    LVL 17

    Accepted Solution

    OK, first, to get the image from the timage tabl;e in the same query, change your SQL on line 4 to this:

    $res=mysql_query('SELECT,t.tdstitle,t.subheading,t.supplier,t.dat,t.category,t.imageid,,i.image FROM technicaldata as t, timages as i WHERE t.category="2" and t.imageid = ORDER BY t.tdstitle DESC',$dbh) or die(mysql_error());

    #this assumes your timages table has an "id" collumn that matches the imageid collumn in technicaldata, and that you store the image in a collumn called "image" in the timages table AND that you "store" only the image name and not the actual image as a blob.

    So, now your SQL will return all the data as previous but in addition, you will laos now have the link between the tecnical data you queried and it's associated image from the timages table, as well as the image name.

    Now, you need to first get the image name from your query - insert this on line 9 of your code:

    $imagename= $val['image'];

    Now we need to check if the image is a .gif or something else:

    $extension = substr($imagename,-3);
    This returns the last three characters of the filename.

    Now we can check to see if it matches "gif" or "GIF" or "Gif" to then either display or not.

    if (($extension == "gif") || ($extension == "GIF") || ($extension == "Gif)) {

    } else {

     You can ajust your code on lines 10 and 11 using this.

    LVL 1

    Author Closing Comment

    That was great thanks... It was the selecting from two tables that was confusing me, but your clear explanasion did it... Perfect Thanks!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now