Jump to content

  • Log in with Facebook Log in with Twitter Log In with Google      Sign In   
  • Create Account

Subscribe to HRA Now!

 



Are you a Google Analytics enthusiast?

Share and download Custom Google Analytics Reports, dashboards and advanced segments--for FREE! 

 



 

 www.CustomReportSharing.com 

From the folks who brought you High Rankings!



Photo
- - - - -

Sql Problem


  • Please log in to reply
12 replies to this topic

#1 madams

madams

    HR 5

  • Active Members
  • PipPipPipPipPip
  • 504 posts
  • Location:Costa Blanca, Spain

Posted 09 March 2010 - 08:31 AM

Hi

I am trying to access some images in a mysql database via php.

[attachment=949:DB.JPG]

Just to clarify
listing_photo (is the database)
photo_id (is = to the image file name (i.e. 120.jpg in folder www.mysite.com/holidays/files/photo_big/)
photo_caption_1 (is the image caption)
photo_listing (is the unique property id)

This is my code...

CODE
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Location where the images are stored
$file_path = 'http://www.mysite.com/holidays/files/photo_big/';

// Fetch the data for the pictures
$sql = "SELECT `photo_id`, `photo_caption_1` , `photo_listing`
        FROM `listing_photo`
        WHERE `photo_listing`= 127
        LIMIT 10";
$result = mysql_query($sql) or trigger_error(mysql_query(), E_USER_ERROR);

// Display each picture
while($row = mysql_fetch_assoc($row))
{
    $src = $file_path . $row['photo_id'] . ".jpg";

    echo '<div class="Image">';
   * echo "<img src="{$src}" alt="{$row['photo_caption_1']}" title="{$row['photo_caption_1']}"><br>"; *
    echo "<span>{$row['photo_caption_1']}</span>";
    echo '</div>';
}
?>


I am getting this error...
CODE
Parse error: syntax error, unexpected '{', expecting ',' or ';' in /usr/local/home/spainhs/www/holidays/templates/english/public/test.php on line 38


Line 38 is the one with *...*.

I have found this advice...
CODE
The error is caused by mismatching quote tags.
That is, when you wrap a string in double-quotes, that string can not contain double-quotes, or they will end the string early. - You can get past this by escaping them (adding a \ in front of them).


Can anyone guide me on this.

#2 Scottie

Scottie

    Psycho Mom

  • Admin
  • 6,294 posts
  • Location:Columbia, SC

Posted 09 March 2010 - 08:44 AM

Try

echo "<img src=\"{$src}\" alt=\"{$row['photo_caption_1'}\" title=\"{$row['photo_caption_1']}\"><br>";

although I don't think that will give you the results you want. "" is a literal, so I think you'll get the actual variable code printing out instead of the value that you want. I think you'll want something more like this:

echo "<img src=\".'{$src}'.\" alt=\".'{$row['photo_caption_1'}'.\" title=\".'{$row['photo_caption_1']}'.\"><br>";

That should tell the program to parse that variable and return the value, I believe.

In that same context, you'd want to change the following line to:

echo "<span>.'{$row['photo_caption_1']}'.</span>";

#3 madams

madams

    HR 5

  • Active Members
  • PipPipPipPipPip
  • 504 posts
  • Location:Costa Blanca, Spain

Posted 09 March 2010 - 09:15 AM

Hi Scottie

I changed the code to...

CODE
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Location where the images are stored
$file_path = 'http://www.mysite.com/holidays/files/photo_big/';

// Fetch the data for the pictures
$sql = "SELECT `photo_id`, `photo_caption_1` , `photo_listing`
        FROM `listing_photo`
        WHERE `photo_listing`= 127
        LIMIT 10";
$result = mysql_query($sql) or trigger_error(mysql_query(), E_USER_ERROR);

// Display each picture
* while($row = mysql_fetch_assoc($row)) *
{
    $src = $file_path . $row['photo_id'] . ".jpg";

    echo '<div class="Image">';
echo "<img src=\".'{$src}'.\" alt=\".'{$row['photo_caption_1]'}'.\" title=\".'{$row['photo_caption_1']}'.\"><br>";
    echo "<span>.'{$row['photo_caption_1']}'.</span>";
    echo '</div>';
}
?>


but now getting the error on *...*

CODE
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /www/holidays/templates/english/public/test.php on line 33

Edited by madams, 09 March 2010 - 09:25 AM.


#4 Mhoram

Mhoram

    HR 4

  • Active Members
  • PipPipPipPip
  • 114 posts
  • Location:Quincy, Illinois, USA

Posted 09 March 2010 - 10:32 AM

I'm not really a PHP expert (learned it under protest), so I'm not sure why you're putting braces around your {variables}, or whether that's the problem, but it's at least unnecessary. To make things easier, get rid of those echo statements and separate your code from your text, and interpolate the necessary variables with <?=?> :

CODE
// Display each picture
while($row = mysql_fetch_assoc($row)){
    $src = $file_path . $row['photo_id'] . ".jpg";
?>

    <div class="Image">
    <img src="<?=$src?>" alt="<?=$row['photo_caption_1']?>" title="<?=$row['photo_caption_1']?>"><br>
    <span><?=$row['photo_caption_1']?></span>
    </div>

<?php } ?>

See if that helps. If nothing else, it gets rid of the backslashed quote mess you see in a lot of PHP. When you have a series of echo/print statements like that, there's usually a better/cleaner way to do it.

Oh, and your most recent error message ("supplied argument not valid mysql resource") is because you're putting the return value of the mysql call in $result, but not using that in mysql_fetch_assoc(). Instead you're calling it on $row, which is null at that point. Should be "$row = mysql_fetch_assoc($result)".

Edited by Mhoram, 09 March 2010 - 10:39 AM.


#5 Scottie

Scottie

    Psycho Mom

  • Admin
  • 6,294 posts
  • Location:Columbia, SC

Posted 09 March 2010 - 10:34 AM

Madams, try taking out the extra stuff like alts and titles and get the basic image pulling up first. When I'm troubleshooting, I start with the very basic stuff and keep adding things back in until it breaks... see if you can just get the images alone to display.

#6 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 09 March 2010 - 10:42 AM

Maybe I'm missing something because I'm just a tad stressed and very, very tired, but I don't see where $row is being defined. Sure there's a $row = mysql_fetch_assoc() but it again reference the $row variable to set the $row variable. Meaning it'll never get set.

Shouldn't the While like look like the following instead?

CODE
while($row = mysql_fetch_assoc($result))


#7 madams

madams

    HR 5

  • Active Members
  • PipPipPipPipPip
  • 504 posts
  • Location:Costa Blanca, Spain

Posted 09 March 2010 - 10:46 AM

I altered "$row = mysql_fetch_assoc($result)" as per Morham advice and got this...

(Randy, missed your post)

Just need to get the image urlīs done.

This is the code now.
CODE
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Location where the images are stored
$file_path = 'http://www.spain-holiday-sun.com/holidays/files/photo_big/';

// Fetch the data for the pictures
$sql = "SELECT `photo_id`, `photo_caption_1` , `photo_listing`
        FROM `listing_photo`
        WHERE `photo_listing`= 127
        LIMIT 10";
$result = mysql_query($sql) or trigger_error(mysql_query(), E_USER_ERROR);

// Display each picture
while($row = mysql_fetch_assoc($result))
{
    $src = $file_path . $row['photo_id'] . ".jpg";

    echo '<div class="Image">';
    echo "<img src=\".'{$src}'.\" alt=\".'{$row['photo_caption_1']}'.\" title=\".'{$row['photo_caption_1']}'.\"><br>";
    echo "<span>.'{$row['photo_caption_1']}'.</span>";
    echo '</div>';
}
?>


#8 Randy

Randy

    Convert Me!

  • Moderator
  • 17,540 posts

Posted 09 March 2010 - 10:57 AM

You're close now. There are just some syntax issues. Name some '. and .' getting added because of the use of \" escapes.

Looks like you missed that echo line Mhoram recommended. So try changing the While section from:

CODE
while($row = mysql_fetch_assoc($result))
{
    $src = $file_path . $row['photo_id'] . ".jpg";

    echo '<div class="Image">';
    echo "<img src=\".'{$src}'.\" alt=\".'{$row['photo_caption_1']}'.\" title=\".'{$row['photo_caption_1']}'.\"><br>";
    echo "<span>.'{$row['photo_caption_1']}'.</span>";
    echo '</div>';
}
?>


To read:

CODE
while($row = mysql_fetch_assoc($result)){
    $src = $file_path . $row['photo_id'] . ".jpg";
?>

    <div class="Image">
    <img src="<?=$src?>" alt="<?=$row['photo_caption_1']?>" title="<?=$row['photo_caption_1']?>"><br>
    <span><?=$row['photo_caption_1']?></span>
    </div>

<?php } ?>


#9 madams

madams

    HR 5

  • Active Members
  • PipPipPipPipPip
  • 504 posts
  • Location:Costa Blanca, Spain

Posted 09 March 2010 - 11:06 AM

BINGO !!!!!!!

Thanks guys cheers.gif

It now showing this...

If I could be so bold...

How would I make the images show...

img1 img2
img3 img4

etc

I have looked this up and get...

QUOTE
$number = 2;
if($number % 2 == 0) {
echo "$number is an even number!";
}
else {
echo "$number is an odd number!";
}


Not sure how it fits in.

#10 Mhoram

Mhoram

    HR 4

  • Active Members
  • PipPipPipPip
  • 114 posts
  • Location:Quincy, Illinois, USA

Posted 09 March 2010 - 12:13 PM

Well, there are a couple different ways you could get your divs to line up in rows of two: change the odd numbered ones to display:inline, or perhaps float them left. (Floating still confuses me; I have to experiment with it to get it right.) A better option might be to put them in an unordered list, and float those left, and then the list should expand to fit your browser window, which might be one image or three.

I don't want to be rude, but it sounds like you're trying to program a web site with no knowledge of programming or HTML coding, which is a little like trying to rebuild your car's engine without knowing what pistons or wrenches are. If this isn't a one-time thing, I'd recommend that you pick up some books or take a class on PHP/MySQL and HTML/CSS. It's going to be pretty frustrating if you have to ask forums for help with every step along the way.

#11 madams

madams

    HR 5

  • Active Members
  • PipPipPipPipPip
  • 504 posts
  • Location:Costa Blanca, Spain

Posted 09 March 2010 - 01:03 PM

No Mahoram, not a complete site. nah.gif

I bought some dodgy software and iīm trying to fix a couple of things.

Thanks for the help.


#12 tcolling

tcolling

    HR 3

  • Active Members
  • PipPipPip
  • 50 posts

Posted 09 March 2010 - 02:22 PM

Beautiful villa, by the way! biggrin.gif

#13 madams

madams

    HR 5

  • Active Members
  • PipPipPipPipPip
  • 504 posts
  • Location:Costa Blanca, Spain

Posted 13 March 2010 - 07:07 AM

After a lot of seaching and help I have come up with this code which displays this output.

There is an error in the code because the images display incorrectly.

i.e.
img1 img2
img2 img3
img4 img5
img3 img6

CODE
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Location where the images are stored
$file_path = 'http://www.mysite.com/holidays/files/photo_big/';

$sql = mysql_query("SELECT photo_id, photo_caption_1, photo_listing
        FROM listing_photo
        WHERE photo_listing = 123
        ORDER BY `photo_status_main` <> 'main'
        LIMIT 10");

if (mysql_num_rows($sql) > 0) {
  // Default numver of columns
  $num_cols = 2;
  
  while ($row = mysql_fetch_array($sql)) {
    $items[] = array('photo_id' => $row['photo_id'], 'photo_caption_1' => $row['photo_caption_1']);
  }
  
  // Number of items in the array
  $num_items = count($items);
  
  // Number of rows
  $num_rows = ceil($num_items / $num_cols);
  
  // Begin HTML table
  echo '<table width="70%">';
    
  for ($row = 1; $row < $num_rows; $row++) {
    $cell = 0;
    
    // Start each new row
    echo '<tr>';
    
    for ($col = 1; $col <= $num_cols; $col++) {
      echo '<td>';
      if ($col === 1) {
        $cell += $row;
        echo '<div class="Image"><img src="'. $file_path . $items[$cell - 1]['photo_id'] .'.jpg" alt="'. $items[$cell - 1]['photo_caption_1'] .'" title="'. $items[$cell - 1]['photo_caption_1'] .'" height="243" width="330"/>';
        echo '<br />';
        echo '<span><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#000000">'. $items[$cell - 1]['photo_caption_1'] .'</font></span></div>';
      }
      else {
        $cell += $row;
        echo '<div class="Image"><img src="'. $file_path . $items[$cell - 1]['photo_id'] .'.jpg" alt="'. $items[$cell - 1]['photo_caption_1'] .'" title="'. $items[$cell - 1]['photo_caption_1'] .'" height="243" width="330"/>';
        echo '<br />';
        echo '<span><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#000000">'. $items[$cell - 1]['photo_caption_1'] .'</font></span></div>';
      }
      echo '</td>';
    }
    echo '</tr>';
  }
  
  echo '</table>';
}
?>


Can anyone spot where the problem is?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

We are now a read-only forum.
 
No new posts or registrations allowed.