You are here:  » Trying to get an image from a database


Trying to get an image from a database

Submitted by mrw2020 on Sat, 2009-06-06 15:56 in

Hi everyone

I wonder if any has an idea how I can get an image from a database and display it in my xml feed result, I know I have had a huge amount of help already but I have tried to use the MySQL select statement as below; can anyone tell me where I'm going wrong?

Best Wishes
Madeleine

<?php
require_once("../mysql_connect.php");
  require(
"magicParser/MagicParser.php");
    function 
getStarSign()
  {
    
$zodiac[356] = "Capricorn";
    
$zodiac[326] = "Sagittarius";
    
$zodiac[296] = "Scorpio";
    
$zodiac[266] = "Libra";
    
$zodiac[235] = "Virgo";
    
$zodiac[203] = "Leo";
    
$zodiac[172] = "Cancer";
    
$zodiac[140] = "Gemini";
    
$zodiac[111] = "Taurus";
    
$zodiac[78]  = "Aries";
    
$zodiac[51]  = "Pisces";
    
$zodiac[20]  = "Aquarius";
    
$zodiac[0]   = "Capricorn";
    
$dayOfTheYear date("z");
    foreach(
$zodiac as $day => $sign) if ($dayOfTheYear $day) break;
    return 
$sign;
  }
    function 
RecordHandler($record)
  {
    global 
$currentStarSign;
    if (
$record["TITLE"] == $currentStarSign)
    {
      print 
"<p>".$record["DESCRIPTION"]."</p>";
      return 
TRUE// stop parsing
    
}
  }
  
$currentStarSign getStarSign();
  
$sql mysql_query("SELECT image FROM signs WHERE title == $currentStarSign");
  
$result mysql_query($sql);
  print 
"<br /><strong>Sign of the Month:</strong><br /><h2>"."<img src=uploads/{$result['image']} />" .$currentStarSign." - your stars for today</h2>";
  
MagicParser_parse("http://horoscopeservices.co.uk/daily_delivery/xmlaccess.asp?uid=898723781","RecordHandler","xml|RSS/CHANNEL/ITEM/");
?>

Submitted by support on Sat, 2009-06-06 17:33

Hi Madeleine,

Your code is very close, but regarding these 3 lines:

  $sql = mysql_query("SELECT image FROM signs WHERE title == $currentStarSign");
  $result = mysql_query($sql);
  print "<br /><strong>Sign of the Month:</strong><br /><h2>"."<img src=uploads/{$result['image']} />" .$currentStarSign." - your stars for today</h2>";

Before you can use $result, you have to fetch the result, and the easiest way is to fetch it as an associative array using mysql_fetch_array($result,MYSQL_ASSOC). In addition, it is always good practice to surround string constants in single-quotes in order to delimit the text from the actual SQL statement. Finally, within SQL only a single = sign is required for comparison, unlike PHP itself! Taking these into account, try the following as a replacement for the above section of your code:

  $sql = mysql_query("SELECT image FROM signs WHERE title = '".$currentStarSign."'");
  $result = mysql_query($sql);
  $row = mysql_fetch_array($result,MYSQL_ASSOC);
  print "<br /><strong>Sign of the Month:</strong><br /><h2>"."<img src='uploads/".$row["image"]."' />" .$currentStarSign." - your stars for today</h2>";

Hope this helps!
Cheers,
David.

Submitted by mrw2020 on Sat, 2009-06-06 21:27

Many thanks David, unfortunately I'm getting the following error message,

'Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\wamp\www\astroserve\noname5.php on line 41'

any ideas?

By the way I really appreciate your quick response - didn't expect one so soon.

Best wishes
Madeleine

Submitted by support on Sun, 2009-06-07 06:02

Hi Madeleine,

That would indicate that the SQL failed, so check that the query is correct for the table etc. One thing that is worth doing is to print the actual MySQL error message; for example:

  $sql = mysql_query("SELECT image FROM signs WHERE title = '".$currentStarSign."'");
  $result = mysql_query($sql);
  if (!$result) die(mysql_error());
  $row = mysql_fetch_array($result,MYSQL_ASSOC);
  print "<br /><strong>Sign of the Month:</strong><br /><h2>"."<img src='uploads/".$row["image"]."' />" .$currentStarSign." - your stars for today</h2>";

Cheers,
David.

Submitted by mrw2020 on Sun, 2009-06-07 06:47

Thank you for the tip David - the response I get is 'Query was empty', I've had a look at the table and the 'blob' and kb size is there, also the images are in the uploads folder - I'm a bit stumped any ideas?

Madeleine

Submitted by support on Sun, 2009-06-07 07:07

Hi Madeleine,

I just spotted that mysql_query is being used twice in the above code; it should be:

  $sql = "SELECT image FROM signs WHERE title = '".$currentStarSign."'";
  $result = mysql_query($sql);

If the table `signs` has the fields `image` and `title` it should work fine!

Cheers,
David.

Submitted by mrw2020 on Sun, 2009-06-07 07:40

Well what can I say! I was surely having a senior moment, it was the spelling in my table that was wrong it now works a treat. Again thank you very very much

Best Wishes
Madeleine